- 使用谷歌的Gson https://github.com/google/gson
- 使用阿里的fastJson https://github.com/alibaba/fastjson
谷歌的Gson:
//list轉(zhuǎn)換為json
Gson gson = new Gson();
List<Person> persons = new ArrayList<Person>();
String str = gson.toJson(persons);
//json轉(zhuǎn)換為list
Gson gson = new Gson();
List<Person> persons = gson.fromJson(str, new TypeToken<List<Person>>(){}.
getType());
阿里的fastJson:
//list轉(zhuǎn)換為json
List<CustPhone> list = new ArrayList<CustPhone>();
String str=JSON.toJSON(list).toString();
//json轉(zhuǎn)換為list
List<Person> list = new ArrayList<Person>();
list = JSONObject.parseArray(jasonArray, Person.class);