需要 GSON 返回一个 Java JSONArray
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10020371/
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
need GSON to return a Java JSONArray
提问by Tom
I'm not able to return a JSONArray, instead my object appears to be a String. the value of myArrayis the same value as jsonString. The object is a String object and not a JSONArray. and both jsonStringand myArrayprnt:
我无法返回 JSONArray,而是我的对象似乎是一个字符串。myArray的值与jsonString的值相同。该对象是一个 String 对象,而不是一个JSONArray。以及jsonString和myArrayprnt:
[{"id":"100002930603211",
"name":"Aardvark Jingleheimer",
"picture":"shortenedExample.jpg" },
{"id":"537815695",
"name":"Aarn Mc",
"picture":"shortendExample.jpg" },
{"id":"658471072",
"name":"Adrna opescu",
"picture":"shortenedExample.jpg"
}]
How can I convert this to an actual Java JSONArray? thanks!
如何将其转换为实际的 Java JSONArray?谢谢!
//arrPersons is an ArrayList
Gson gson = new Gson();
String jsonString = gson.toJson(arrPersons);
JsonParser parser = new JsonParser();
JsonElement myElement = parser.parse(jsonString);
JsonArray myArray = myElement.getAsJsonArray();
采纳答案by Parker
I think you can do what you want without writing out a json string and then re-reading it:
我认为你可以做你想做的事,而无需写出一个 json 字符串然后重新阅读它:
List<Person> arrPersons = new ArrayList<Person>();
// populate your list
Gson gson = new Gson();
JsonElement element = gson.toJsonTree(arrPersons, new TypeToken<List<Person>>() {}.getType());
if (! element.isJsonArray()) {
// fail appropriately
throw new SomeException();
}
JsonArray jsonArray = element.getAsJsonArray();
回答by Tom
public JSONArray getMessage(String response){
ArrayList<Person> arrPersons = new ArrayList<Person>();
try {
// obtain the response
JSONObject jsonResponse = new JSONObject(response);
// get the array
JSONArray persons=jsonResponse.optJSONArray("data");
// iterate over the array and retrieve single person instances
for(int i=0;i<persons.length();i++){
// get person object
JSONObject person=persons.getJSONObject(i);
// get picture url
String picture=person.optString("picture");
// get id
String id=person.optString("id");
// get name
String name=person.optString("name");
// construct the object and add it to the arraylist
Person p=new Person();
p.picture=picture;
p.id=id;
p.name=name;
arrPersons.add(p);
}
//sort Arraylist
Collections.sort(arrPersons, new PersonSortByName());
Gson gson = new Gson();
//gson.toJson(arrPersons);
String jsonString = gson.toJson(arrPersons);
sortedjsonArray = new JSONArray(jsonString);
} catch (JSONException e) {
e.printStackTrace();
}
return sortedjsonArray;
}
public class PersonSortByName implements Comparator<Person>{
public int compare(Person o1, Person o2) {
return o1.name.compareTo(o2.name);
}
}
public class Person{
public String picture;
public String id;
public String name;
}