import org.json.simple.JSONObject; class JsonEncodeDemo { public static void main(String[] args) { JSONObject obj = new JSONObject(); obj.put("title", "JSON教程"); obj.put("author", "C语言中文网"); obj.put("url", "http://task.lmcjl.com/"); obj.put("hits", 100); System.out.print(obj); } }编译并执行上述程序,结果如下:
{"hits":100,"author":"C语言中文网","title":"JSON教程","url":"http:\/\/task.lmcjl.com\/"}
import org.json.simple.JSONObject; import org.json.simple.JSONArray; import org.json.simple.parser.ParseException; import org.json.simple.parser.JSONParser; class JsonDecodeDemo { public static void main(String[] args) { JSONParser parser = new JSONParser(); String s = "[0,{\"hits\":100,\"author\":\"C语言中文网\",\"title\":\"JSON教程",\"url\":\"http://task.lmcjl.com/\"}]"; try{ Object obj = parser.parse(s); JSONArray array = (JSONArray)obj; System.out.println(array.get(1)); System.out.println(); }catch(ParseException pe) { System.out.println("position: " + pe.getPosition()); System.out.println(pe); } } }编译并执行上述程序,结果如下:
{"hits":100,"author":"C语言中文网","title":"JSON教程","url":"http:\/\/task.lmcjl.com\/"}
本文链接:http://task.lmcjl.com/news/14037.html