package com.jtjdxh.json.test; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class Test { public void Test(){ } public static void main(String[] args) { //创建json对象 JSONObject jsonObject = new JSONObject(); jsonObject.put("n", "niaho"); System.out.println(jsonObject.toString()); //接收json串并解析 JSONObject myJsonObject = JSONObject.fromObject(jsonObject.toString()); Map map = (HashMap) JSONObject.toBean(myJsonObject, HashMap.class); System.out.println(map.get("n")); //使用list创建JSONArray List list = new ArrayList(); list.add("nihao"); list.add("tian"); list.add("ya"); list.add(myJsonObject); JSONArray jsonArray = JSONArray.fromObject(list); System.out.println(jsonArray.get(1)); JSONObject jsonObject2 =(JSONObject)jsonArray.get(3); System.out.println(jsonObject2.toString()); } }