【问题标题】:Using Json Data in hashmap , For exact reference to json Array data .And using it as a string array for numberpicker在 hashmap 中使用 Json 数据,准确引用 json 数组数据。并将其用作 numberpicker 的字符串数组
【发布时间】:2017-10-19 13:20:33
【问题描述】:

我正在开发 android 应用程序并使用数字选择器来显示数据。 我有这个 json 作为字符串:

{"Acura": ["CL", "ILX", "Integra", "Legend", "MDX", "NSX", "RDX", "RL", "RLX", "RSX", "SLX", "TL", "TLX", "TSX", "Vigor", "ZDX", "Other Acura Models"],
            "Alfa": ["164", "4C", "8C Competizione", "Giulia", "GTV-6", "Milano", "Spider", "Stelvio", "Other Alfa Romeo Models"],
            "AMC": ["Alliance", "Concord", "Eagle", "Encore", "Spirit", "Other AMC Models"],
            "Aston": ["DB11", "DB7", "DB9", "DBS", "Lagonda", "Rapide", "Rapide S", "V12 Vantage", "V8 Vantage", "Vanquish"]} .

我试图在单个数组列表中获得像讴歌、阿尔法、AMC、阿斯顿这样的对象。我被困在如何在另一个数字选择器中获取讴歌模型, 那里我需要传递数据的字符串数组。

所以这是我模棱两可的情况。

现在我需要的是,我有对象字符串数组,这样每当我选择 Acura 时,另一个选择器应该显示 Acura 模型。

如果我选择 Alfa,其他选择器会显示 Alfa 模型。为此,我认为我需要有 hashmap。如果有人可以帮助我解决这种情况,我仍然不知道如何实施。谢谢。

【问题讨论】:

  • HashMap> ?您只需将它与 Gson 一起使用。很简单。

标签: java android json hashmap


【解决方案1】:

您需要像这样将数据存储在Map<String,List<String>>

 Type mapType = new TypeToken<Map<String, List<String>>>() {}.getType();
 Map<String, List<String>> source = new Gson().fromJson('your_json_string_here', mapType);

然后像这样迭代它:

for (Map.Entry<String, List<String>> entry : source.entrySet()) {

            String key = entry.getKey();
            List<String> values = entry.getValue();

            //Here you can write your code to do whatever you want
        }

【讨论】:

  • 我如何存储它,我有单个 json 字符串 ..?
  • 我想在这个HashMap中存储数据 carsDATA = new HashMap();
  • 你需要用gson解析它我会编辑答案给你代码
  • 如果您对此回答有疑问,请点赞我的评论并接受它作为正确答案
猜你喜欢
  • 2014-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多