1、引入json-org.jar包
2、
1)json解析
String jsonString="{\"users\":[{\"loginname\":\"张三\",\"password\":\"123\",\"email\":\"16666666@qq.com\"},{\"loginname\":\"李四\",\"password\":\"333\",\"email\":\"777887@qq.com\"}]}";
JSONObject json= new JSONObject(jsonString); JSONArray jsonArray=json.getJSONArray("users"); for(int i=0;i<jsonArray.length();i++){ JSONObject user=(JSONObject) jsonArray.get(i); String userName=(String) user.get("loginname"); String password=(String) user.get("password"); System.out.println("userName:"+userName+"------------password:"+password); }2)//json解析
String str = "{\"response\":{\"data\":[{\"address\":\"郑州动物园\",\"province\":\"河南省\",\"district\":\"金水区\",\"city\":\"郑州\"}]},\"status\":\"ok\"}";
JSONObject dataJson=new JSONObject(str); JSONObject response=dataJson.getJSONObject("response"); JSONArray data=response.getJSONArray("data"); JSONObject info=data.getJSONObject(0); String province=info.getString("province"); String city=info.getString("city"); String district=info.getString("district"); String address=info.getString("address"); System.out.println("province:"+province+"--------city:"+city+"------------district:"+district+"--------------address:"+address);
3)//json解析数组转换成对象
String str = "[{\"name\":\"array\",\"id\":123456,\"date\":\"2013-4-13 12:36:54\"},{\"name\":\"tom\",\"id\":123,\"date\":\"2013-4-13 12:36:54\"}]";
JSONArray jsonArray = new JSONArray(str); int iSize = jsonArray.length(); System.out.println("Size:" + iSize); for (int i = 0; i < iSize; i++) { JSONObject jsonObj = jsonArray.getJSONObject(i); System.out.println("name=" + jsonObj.get("name")); System.out.println("id=" + jsonObj.get("id")); System.out.println("date=" + jsonObj.get("date")); System.out.println(); }