1、单个json数据的封装方法:
/*** 封装单独的Json数据* @return*/public String setJson() throws JSONException {JSONObject jsonObject=new JSONObject();JSONObject jsonstring=new JSONObject();jsonObject.put("name","张三");jsonObject.put("sex","男");jsonObject.put("age",23);//给json数据加上keyjsonstring.put("Person",jsonObject);return jsonstring.toString();}
解析单个json数据的方法:
/*** 解析单个json数据*/public void analysisJSON(String json) throws JSONException {JSONObject jsonObject=new JSONObject(json);//通过key获取json数据JSONObject jsonobj=jsonObject.getJSONObject("Person");String name=jsonobj.getString("name");String sex=jsonobj.getString("sex");int age=jsonobj.getInt("age");System.out.println("姓名:"+name+",性别:"+sex+",年龄:"+age);}
2、封装JSON数组数据方法: