用Gson把JSON数组parsing成java.util.List

我有一个JsonObject名为"mapping"与以下内容:

 { "client": "127.0.0.1", "servers": [ "8.8.8.8", "8.8.4.4", "156.154.70.1", "156.154.71.1" ] } 

我知道我可以得到数组"servers"

 mapping.get("servers").getAsJsonArray() 

现在我想parsingJsonArray到一个java.util.List

什么是最简单的方法来做到这一点?

绝对最简单的方法是使用fromJson() Gson默认parsing函数。

这个函数的实现适用于当你需要反序列化到fromJson(JsonElement json, Type typeOfT)任何ParameterizedType (例如,任何List fromJson(JsonElement json, Type typeOfT)

在你的情况下,你只需要得到一个List<String>Type ,然后把JSON数组parsing成这个Type ,就像这样:

 import java.lang.reflect.Type; import com.google.gson.reflect.TypeToken; JsonElement yourJson = mapping.get("servers"); Type listType = new TypeToken<List<String>>() {}.getType(); List<String> yourList = new Gson().fromJson(yourJson, listType); 

在你的情况yourJson是一个JsonElement ,但它也可以是一个String ,任何ReaderJsonReader

你可能想看看Gson API文档 。

下面的代码是使用com.google.gson.JsonArray 。 我列出了列表中元素的数量以及列表中的元素

 import java.util.ArrayList; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonParser; public class Test { static String str = "{ "+ "\"client\":\"127.0.0.1\"," + "\"servers\":[" + " \"8.8.8.8\"," + " \"8.8.4.4\"," + " \"156.154.70.1\"," + " \"156.154.71.1\" " + " ]" + "}"; public static void main(String[] args) { // TODO Auto-generated method stub try { JsonParser jsonParser = new JsonParser(); JsonObject jo = (JsonObject)jsonParser.parse(str); JsonArray jsonArr = jo.getAsJsonArray("servers"); //jsonArr. Gson googleJson = new Gson(); ArrayList jsonObjList = googleJson.fromJson(jsonArr, ArrayList.class); System.out.println("List size is : "+jsonObjList.size()); System.out.println("List Elements are : "+jsonObjList.toString()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 

OUTPUT

 List size is : 4 List Elements are : [8.8.8.8, 8.8.4.4, 156.154.70.1, 156.154.71.1] 

我在这里阅读Gson官方网站的解决scheme

而这个代码给你:

  String json = "{"client":"127.0.0.1","servers":["8.8.8.8","8.8.4.4","156.154.70.1","156.154.71.1"]}"; JsonObject jsonObject = new Gson().fromJson(json, JsonObject.class); JsonArray jsonArray = jsonObject.getAsJsonArray("servers"); String[] arrName = new Gson().fromJson(jsonArray, String[].class); List<String> lstName = new ArrayList<>(); lstName = Arrays.asList(arrName); for (String str : lstName) { System.out.println(str); } 

显示器上显示的结果:

 8.8.8.8 8.8.4.4 156.154.70.1 156.154.71.1 

看看这个,我相信这是你需要的。

 public class Test { public static void main(String[] args) { // this part is just to create the JSON object in question, so I am sure you are already having it. JsonObject mappings = new JsonObject(); JsonElement e = new JsonPrimitive("127.0.0.1"); mappings.add("client", e); JsonArray servers = new JsonArray(); JsonElement s1 = new JsonPrimitive("8.8.8.8"); JsonElement s2 = new JsonPrimitive("8.8.4.4"); JsonElement s3 = new JsonPrimitive("156.154.70.1"); JsonElement s4 = new JsonPrimitive( "156.154.71.1"); servers.add(s1); servers.add(s2); servers.add(s3); servers.add(s4); mappings.add("servers", servers); // json creation ends. // split the resultant string based on comma and then convert the resultant array into a list. String[] split = mappings.get("servers").getAsJsonArray().toString().split(","); List<String> myList = new ArrayList<String>(); Collections.addAll(myList, split); // again the following part is to demonstrate that the strings were populated in the list. for(String l : myList){ System.out.println(l); } } }