Gson解析json陣列
阿新 • • 發佈:2019-02-03
package test; import java.lang.reflect.Type; import java.util.LinkedList; import java.util.List; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; public class Test { public static void main(String[] args) { String json = "[{\"key\":\"key1\",\"value\":\"value1\"},{\"key\":\"key2\",\"value\":\"value2\"}]"; Type listType = new TypeToken<LinkedList<A>>(){}.getType(); Gson gson = new Gson(); LinkedList<A> la = gson.fromJson(json, listType); System.out.println(la.size()); } class A { private String key; private String value; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } } }