1.JSON jdk解析方式
所需要加包
json-lib-2.2.2-jdk15.jar
package com.learn.json;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Json_Jdk {
//json字符串
private static String diskListContent = "[{\"n1\":\"asd\",\"n2\":22,\"n3\":\"45.40GB\"," + "\"n4\":\"qwerty\",\"n5\":\"asd\",}," + "{\"n1\":\"local\","
+ "\"n2\":1,\"n3\":\"279.40GB\",\"n4\":\"ST3300656SS\",\"n5\":\"\\/devm\\/d0\"}]";
/**
* json字符串转换成 map
* @param rsContent
* @return
* @throws Exception
*/
private static List<Map<String, String>> jsonStringToList(String rsContent) throws Exception{
JSONArray arry = JSONArray.fromObject(rsContent);
System.out.println("json字符串转换成 map");
List<Map<String, String>> rsList = new ArrayList<Map<String, String>>();
for (int i = 0; i < arry.size(); i++){
JSONObject jsonObject = arry.getJSONObject(i);
Map<String, String> map = new HashMap<String, String>();
for (Iterator<?> iter = jsonObject.keys(); iter.hasNext();){
String key = (String) iter.next();
String value = jsonObject.get(key).toString();
map.put(key, value);
}
rsList.add(map);
}
return rsList;
}
public static void main(String[] args) throws Exception {
List<Map<String, String>> list1 = jsonStringToList(diskListContent);
System.out.println("json字符串成map");
for (Map<String, String> m : list1){
System.out.println(m);
}
System.out.println("map转换成json字符串");
for (Map<String, String> m : list1){
//初始化JSONArray对象,并添加数据
JSONArray arry = JSONArray.fromObject(m);
//JSONArray jsonArray = JSONArray.fromObject(m);
System.out.println(arry.toString());
}
System.out.println("list转换成json字符串");
JSONArray jsonArray2 = JSONArray.fromObject(list1);
System.out.println(jsonArray2.toString());
}
}
1.JSON ORG解析方式
org.json.jar
package com.json.learn;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
public class Json_org {
//json字符串
private static String diskListContent = "[{\"n1\":\"asd\",\"n2\":22,\"n3\":\"45.40GB\"," + "\"n4\":\"qwerty\",\"n5\":\"asd\",}," + "{\"n1\":\"local\","
+ "\"n2\":1,\"n3\":\"279.40GB\",\"n4\":\"ST3300656SS\",\"n5\":\"\\/devm\\/d0\"}]";
/**
* json字符串转换成 map
* @param rsContent
* @return
* @throws Exception
*/
private static List<Map<String, String>> jsonStringToList(String rsContent) throws Exception{
JSONArray arry = new JSONArray(rsContent);
System.out.println("json字符串转换成 map");
List<Map<String, String>> rsList = new ArrayList<Map<String, String>>();
for (int i = 0; i < arry.length(); i++){
JSONObject jsonObject = arry.getJSONObject(i);
Map<String, String> map = new HashMap<String, String>();
for (Iterator<?> iter = jsonObject.keys(); iter.hasNext();){
String key = (String) iter.next();
String value = jsonObject.get(key).toString();
map.put(key, value);
}
rsList.add(map);
}
return rsList;
}
public static void main(String[] args) throws Exception {
List<Map<String, String>> list1 = jsonStringToList(diskListContent);
System.out.println("json字符串成map");
for (Map<String, String> m : list1){
System.out.println(m);
}
System.out.println("map转换成json字符串");
for (Map<String, String> m : list1){
//初始化JSONArray对象,并添加数据
JSONArray array = new JSONArray();
array.put(m);
//JSONArray jsonArray = JSONArray.fromObject(m);
System.out.println(array.toString());
}
System.out.println("list转换成json字符串");
JSONArray array = new JSONArray();
array.put(list1);
//JSONArray jsonArray2 = JSONArray.fromObject(list1);
System.out.println(array.toString());
}
}