java 将 JSONArray 转换为 arrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30305836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:51:46  来源:igfitidea点击:

Convert JSONArray to arrayList

javaarraysjsonarraylist

提问by azelix

I'm trying to convert a JSONArraywhich looks like this :

我正在尝试转换一个JSONArray看起来像这样的:

{"output":["name":"Name3","URI":"Value3"},{"name":"Name5","URI":"Value5"},{"name":"Name4","URI":"Value4"}]}

Into an arrayList so for example the output of Arr[0][0]will be Name3

进入arrayList 所以例如输出Arr[0][0]将是Name3

I have tried this solution:

我试过这个解决方案:

if (outputs!= null) { 
    int len = outputs.length();
    for (int j=0;j<len;j++){ 
        list.add(outputs.get(j).toString());
    } 
} 
for (String str : list) {               
    System.out.println("Item is: " + str);              
}

But I get the full row : {"name":"Name3","URI":"Value3"}

但我得到了整行: {"name":"Name3","URI":"Value3"}

How can I get each object of my JSONArray?

我怎样才能得到我的每个对象JSONArray

采纳答案by MaxZoom

Aazelix, your Json output seem to be missing opening array bracket. Its correct form is listed below:

Aazelix,您的 Json 输出似乎缺少开放数组括号。其正确形式如下:

{"output":[{"name":"Name3","URI":"Value3"},{"name":"Name5","URI":"Value5"},{"name":"Name4","URI":"Value4"}]}

As for the conversion to POJO

至于转换为POJO

List<MyObj> list = new ArrayList<>();
if (outputs!= null) { 
  int len = outputs.length();
  for (int i=0; i<len; i++) { 
    JSONObject o = (JSONObject) outputs.get(i);
    list.add(new MyObj(o.getString('name'), o.getString('URL')));
  } 
} 
System.out.println("There is " + list.size() + " objects.");


public static final class MyObj {
  final String name;
  final String url;

  public MyObj(String name, String url) {
     this.name = name;
     this.url  = url;
  }
}

回答by Dmitry Ginzburg

It is not specified, which JSON parser do you use, so I suppose you can choose right now and I'll suggest using Gsonas such.

它没有指定,你使用哪个 JSON 解析器,所以我想你现在可以选择,我会建议使用Gson它。

The better solution for deserializing such a structures is creating a special class for each structure, for example:

反序列化此类结构的更好解决方案是为每个结构创建一个特殊的类,例如:

public class NameURIPair {
    private String name;
    private String URI;

    // getters
}

Then your JSON can be deserialized into a class, which holds the resulting Listin it:

然后您的 JSON 可以反序列化为一个类,其中包含结果List

public class Data {
    private List<NameURIPair> output;

    // getter
}

// ...

Data data = new Gson(stringData, Data.class);

Since you've requested the other way, you still can get just the parsed JSON into JsonElementwith JsonParser

由于您以另一种方式请求,您仍然可以将解析的 JSON 转换JsonElementJsonParser

JsonElement root = new JsonParser().parse(stringData);

Although I won't give you the full solution not to appreciate this kind of solutions :-)

虽然我不会给你完整的解决方案不欣赏这种解决方案:-)