最近在完善某网站,遇到个问题,就是JAVA里怎么使用JSON,后来经过努力,最后发现了json-simple这个好东东,下面把使用方法给大家说说。
JSON ENCODE编码
例子1:
//import org.json.simple.JSONObject;
JSONObject obj=new JSONObject();
obj.put("name","foo");
obj.put("num",new Integer(100));
obj.put("balance",new Double(1000.21));
obj.put("is_vip",new Boolean(true));
obj.put("nickname",null);
StringWriter out = new StringWriter();
obj.writeJSONString(out);
String jsonText = out.toString();
System.out.print(jsonText);
结果: {"balance":1000.21,"num":100,"nickname":null,"is_vip":true,"name":"foo"}
例子2:
//import java.util.LinkedHashMap;
//import java.util.Map;
//import org.json.simple.JSONValue;
Map obj=new LinkedHashMap();
obj.put("name","foo");
obj.put("num",new Integer(100));
obj.put("balance",new Double(1000.21));
obj.put("is_vip",new Boolean(true));
obj.put("nickname",null);
String jsonText = JSONValue.toJSONString(obj);
System.out.print(jsonText);
结果: {"name":"foo","num":100,"balance":1000.21,"is_vip":true,"nickname":null}
例子3:
//import java.util.LinkedHashMap;
//import java.util.Map;
//import org.json.simple.JSONValue;
Map obj=new LinkedHashMap();
obj.put("name","foo");
obj.put("num",new Integer(100));
obj.put("balance",new Double(1000.21));
obj.put("is_vip",new Boolean(true));
obj.put("nickname",null);
StringWriter out = new StringWriter();
JSONValue.writeJSONString(obj, out);
String jsonText = out.toString();
System.out.print(jsonText); 结果: {"name":"foo","num":100,"balance":1000.21,"is_vip":true,"nickname":null}
例子4:
//import org.json.simple.JSONArray;
JSONArray list = new JSONArray();
list.add("foo");
list.add(new Integer(100));
list.add(new Double(1000.21));
list.add(new Boolean(true));
list.add(null);
System.out.print(list); 结果["foo",100,1000.21,true,null]
JSON DECODE
例子1:
System.out.println("=======decode=======");
String s="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
Object obj=JSONValue.parse(s);
JSONArray array=(JSONArray)obj;
System.out.println("======the 2nd element of array======");
System.out.println(array.get(1));
System.out.println();
JSONObject obj2=(JSONObject)array.get(1);
System.out.println("======field \"1\"==========");
System.out.println(obj2.get("1"));
s="{}";
obj=JSONValue.parse(s);
System.out.println(obj);
s="[5,]";
obj=JSONValue.parse(s);
System.out.println(obj);
s="[5,,2]";
obj=JSONValue.parse(s);
System.out.println(obj);
例子2:
JSONParser parser=new JSONParser();
System.out.println("=======decode=======");
String s="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
Object obj=parser.parse(s);
JSONArray array=(JSONArray)obj;
System.out.println("======the 2nd element of array======");
System.out.println(array.get(1));
System.out.println();
JSONObject obj2=(JSONObject)array.get(1);
System.out.println("======field \"1\"==========");
System.out.println(obj2.get("1"));
s="{}";
obj=parser.parse(s);
System.out.println(obj);
s="[5,]";
obj=parser.parse(s);
System.out.println(obj);
s="[5,,2]";
obj=parser.parse(s);
System.out.println(obj);
例子3:
String jsonText = "[[null, 123.45, \"a\\tb c\"]}, true";
JSONParser parser = new JSONParser();
try{
parser.parse(jsonText);
}
catch(ParseException pe){
System.out.println("position: " + pe.getPosition());
System.out.println(pe);
}
类库下载地址:http://www.ipkbus.com/down/jsonsimple.tar.gz