JSON 创建和解析

jopen 12年前

1.输出多个对象功能的json代码{"status":1,"name":2,"user":{"sex":"girl","age":1,"name":"cm111"},"game":{"uid":"111","changeid":2,"gamaname":"baby"}}

代码:

publicvoid test1(ActionMapping mapping, ActionForm form,

           HttpServletRequest request, HttpServletResponse response)throws Exception{

       PrintWriter out = response.getWriter();

       JSONObject jsonORG =new JSONObject();

       jsonORG.element("status", 1);

       jsonORG.element("name", 2);

       Map<String , Object> user=new HashMap<String, Object>();

       user.put("name","cymccc");

       user.put("age", 1);

       user.put("sex","girl");

       Map<String , Object> game=new HashMap<String, Object>();

       game.put("uid","111");

       game.put("changeid", 2);

       game.put("gamaname","baby");

       jsonORG.element("user", user);

       jsonORG.element("game", game);

       response.setContentType("application/json;charset=GBK");

       out.print(jsonORG);

    }

2.输出数组功能的json例子

如:{"status":1,"name":2,"list":[{"sex":"girl","age":1,"name":"cym"},{"sex":"baby","age":2,"name":"111"}]}

 

代码:publicvoid test1(ActionMapping mapping, ActionForm form,

           HttpServletRequest request, HttpServletResponse response)throws Exception{

       PrintWriter out = response.getWriter();

       JSONObject jsonORG = new JSONObject();

       jsonORG.element("status", 1);

       jsonORG.element("name", 2);

       Map<String , Object> user=new HashMap<String, Object>();

       user.put("name","cym");

       user.put("age", 1);

       user.put("sex","girl");

       Map<String , Object> game=new HashMap<String, Object>();

       game.put("name","111");

       game.put("age", 2);

       game.put("sex","baby");

      

       List<Object> list=new ArrayList<Object>();

       list.add(user);

       list.add(game);

       jsonORG.put("list", list);

       //jsonORG.element("game", game);

       response.setContentType("application/json;charset=GBK");

       out.print(jsonORG);

    }

 

 

 

 

 

解析各种json

1.如://解析数组类型的json[{'gwcxxid':'1','spsl':'2'},{'gwcxxid':'1','spsl':'2'},{'gwcxxid':'3','spsl':'4'}]

代码:  public static void jiexijson() throws JSONException {

       String sJson = "[{'gwcxxid':'1','spsl':'2'},{'gwcxxid':'1','spsl':'2'},{'gwcxxid':'3','spsl':'4'}]";

       JSONArray jsonArray = new JSONArray(sJson);

       int iSize = jsonArray.length();

       System.out.println("Size:" + iSize);

       for (int i = 0; i < iSize; i++) {

           JSONObject jsonObj = jsonArray.getJSONObject(i);

           System.out.println("[" + i + "]gwcxxid=" + jsonObj.get("gwcxxid"));

           System.out.println("[" + i + "]spsl=" + jsonObj.get("spsl"));

           System.out.println();

       }

    }

 

 

2. // 解析返回路径的json{"status":1,"name":2}

    代码:publicstaticvoid jiexijson2()throws Exception {

       String temIP = "http://localhost:8089/hotelOrderCustom.go?method=test1";

       URLConnection connection = (URLConnection)new URL(temIP)

              .openConnection();

       connection.setDoOutput(true);

       InputStream os = connection.getInputStream();

       // Thread.sleep(100);

       int length = os.available();

       byte[] buff =newbyte[length];

       os.read(buff);

       String s = new String(buff,"gbk");

       System.out.println(s);

       System.out.println(s);

       JSONObject obj = new JSONObject(s);

       System.out.println("status=" + obj.get("status"));

       System.out.println("name=" + obj.get("name"));

       //解析list

      

       String list=obj.get("list").toString();

       JSONArray arrayList=new JSONArray(list);

       int size=arrayList.length();

       for(int i=0;i<size;i++){

           JSONObject jsonObj = arrayList.getJSONObject(i);

           System.out.println("name["+i+"]"+jsonObj.get("name"));

           System.out.println("sex["+i+"]"+jsonObj.get("sex"));

           System.out.println("age["+i+"]]"+jsonObj.get("age"));

       }

       System.out.println();

 

    }

 

 

3. //解析最简单的json{'gwcxxid':'小陈','spsl':'喝了来来来','aihao':'乱七八糟'}

代码:publicstaticvoid jiexijson3()throws JSONException {

       String sJson = "{'gwcxxid':'小陈','spsl':'喝了来来来','aihao':'乱七八糟'}";

       JSONObject obj = new JSONObject(sJson);

       System.out.println("gwcxxid=" + obj.get("gwcxxid"));

       System.out.println("spsl=" + obj.get("spsl"));

       System.out.println("dd=" + obj.get("aihao"));

       System.out.println();

    }

 

4

    //解析{"rspCode":200,"rspMsg":"ok","list":[{"uid":"kaix,"logo":"http:\/\/p.com.cn\/logo\/63\/16\/50_9_1.jpg","level":1,"name":"鍏崇淮搴"}]}

    publicstaticvoid jiexijson4()throws JSONException {

       String sJson = "{'rspCode':200,'rspMsg':'ok','list':[{'uid':'kaixin00','logo':'http://a.gif','level':1,'name':'小布丁'},{'uid':'kaixfff','logo':'http://b.gif','level':2,'name':'小布丁22'}]}";

       //解析不是数组的两个字段

       JSONObject obj = new JSONObject(sJson);

       System.out.println("rspCode=" + obj.get("rspCode"));

       System.out.println("rspMsg=" + obj.get("rspMsg"));

       //解析数组,获取list数组

       String test = obj.get("list").toString();

       JSONArray jsonArray = new JSONArray(test);

       int iSize = jsonArray.length();

       for (int i = 0; i < iSize; i++) {

           JSONObject jsonObj = jsonArray.getJSONObject(i);

           System.out.println("[" + i +"]uid=" + jsonObj.get("uid"));

           System.out.println("[" + i +"]logo=" + jsonObj.get("logo"));

           System.out.println("[" + i +"]level=" + jsonObj.get("level"));

           System.out.println("[" + i +"]name=" + jsonObj.get("name"));

           System.out.println();

       }

    }

 

5

    //解析{"rspCode":200, "rspMsg":"ok", "game":{"uid":"hp061", "trackId":187,

    // "gameId":9, "background":””, "mp3":””, "taptapFile":””}}

    publicstaticvoid jiexijson5()throws JSONException {

       String sJson = "{'rspCode':200, 'rspMsg':'ok', 'game':{'uid':'hpbb', 'trackId':187, 'gameId':9, 'background':'', 'mp3':'', 'taptapFile':''}}";

       JSONObject obj = new JSONObject(sJson);

       System.out.println("rspCode=" + obj.get("rspCode"));

       System.out.println("rspMsg=" + obj.get("rspMsg"));

       String test = obj.get("game").toString();

       JSONObject obj2 = new JSONObject(test);

       System.out.println("uid" + obj2.get("uid"));

       System.out.println("trackId" + obj2.get("trackId"));

       System.out.println("gameId" + obj2.get("gameId"));

       System.out.println("background" + obj2.get("background"));

       System.out.println("mp3" + obj2.get("mp3"));

       System.out.println("taptapFile" + obj2.get("taptapFile"));

 

    }