List 与JSON array 之间的转换

List 与JSON array 之间的转换

List barList = new ArrayList(); 
barList.add("a"); 
barList.add("b"); 
barList.add("c"); 
String json= JSON.toJSONString(barList, true); 
System.out.println(json); 
/**
* 输出[
	"a",
	"b",
	"c"
	]
*/

//JSON array -> List 
List barList1 = JSON.parseArray(json,String.class); 
for (String bar : barList1) { 
System.out.println(bar.toString()); 
} 
/**
* 输出
* a
 * b
 * c
*/

//List -> JSON array 
List barList2 = new ArrayList(); 
barList2.add(new Bar()); 
barList2.add(new Bar()); 
barList2.add(new Bar()); 
String json2= JSON.toJSONString(barList2, true); 
System.out.println(json2); 
/**
* 输出
* [
	{
		"barAge":-914898881,
		"barDate":1494946211588,
		"barName":"name_1374144199"
	},
	{
		"barAge":1101450045,
		"barDate":1494946211588,
		"barName":"name_1196777610"
	},
	{
		"barAge":-1791815270,
		"barDate":1494946211588,
		"barName":"name_-646614218"
	}
  ]
*/

List barList3 = JSON.parseArray(json2,Bar.class); 
for (Bar bar : barList3) { 
System.out.println(bar.toString()); 
} 
/**
* 输出
* Bar{barName='name_1374144199', barAge=-914898881, barDate=Tue May 16 22:50:11 CST 2017}
   Bar{barName='name_1196777610', barAge=1101450045, barDate=Tue May 16 22:50:11 CST 2017}
   Bar{barName='name_-646614218', barAge=-1791815270, barDate=Tue May 16 22:50:11 CST 2017}
*/
} 

来自:http://www.cnblogs.com/DreamDrive/p/5778959.html

你可能感兴趣的:(JSON)