import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; //获取解析的串,如下json串 public static String getJsonString(String urlPath) throws Exception { StringBuffer sb = new StringBuffer(); try { URL url = new URL(urlPath); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(50000); connection.connect(); InputStream inputStream = connection.getInputStream(); //对应的字符编码转换 Reader reader = new InputStreamReader(inputStream, "UTF-8"); BufferedReader bufferedReader = new BufferedReader(reader); String str = null; while ((str = bufferedReader.readLine()) != null) { sb.append(str); } reader.close(); connection.disconnect(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); System.out.println("连接超时。。。"); return "timeout"; } return sb.toString(); }
json字串 :
{"response":"ok","result":{"userList":[{"name":"9S7B_music_user","id":162,"type":2,"date":1375780379000,"pwd":"e10adc3949ba59abbe56e057f20f883e","cyCount":1,"ctCount":1,"myCount":1,"cgCount":21,"gold":11},{"name":"fred26","id":129,"type":2,"date":1375781355000,"pwd":"e10adc3949ba59abbe56e057f20f883e","cyCount":3,"ctCount":1,"myCount":1,"cgCount":7,"gold":3},{"name":"2VPL_music_user","id":170,"type":2,"date":1376032147000,"pwd":"e10adc3949ba59abbe56e057f20f883e","cyCount":1,"ctCount":1,"myCount":1,"cgCount":6,"gold":1},{"name":"T8D8_music_user","id":167,"type":2,"date":1375844980000,"pwd":"e10adc3949ba59abbe56e057f20f883e","cyCount":1,"ctCount":1,"myCount":1,"cgCount":6,"gold":15},{"name":"VFWR_music_user","id":159,"type":2,"date":1375777245000,"pwd":"e10adc3949ba59abbe56e057f20f883e","cyCount":1,"ctCount":1,"myCount":1,"cgCount":5,"gold":5},{"name":"dadaf","id":171,"type":2,"date":1376034139000,"pwd":"e10adc3949ba59abbe56e057f20f883e","cyCount":1,"ctCount":1,"myCount":1,"cgCount":4,"gold":2},{"name":"WGOL_music_user","id":166,"type":2,"date":1375843886000,"pwd":"e10adc3949ba59abbe56e057f20f883e","cyCount":1,"ctCount":1,"myCount":1,"cgCount":4,"gold":2}}]}}
相关方法
/** * 将上述json串转成对象列表 * @param jsonStr * @return * @throws Exception */ public static List jsonListToObjectList(String jsonStr) throws Exception { List<User> userList = new ArrayList<User>(); JSONObject jsonObject = new JSONObject(jsonStr); String response = jsonObject.getString("response"); String json = jsonObject.getString("result"); JSONObject jsonObject2 = new JSONObject(json); if(jsonStr.indexOf("userList") > -1){ String uList = jsonObject2.getString("userList"); if(null != uList){ // JSONObject jsonObject4 = new JSONObject(uList); JSONArray arry = jsonObject2.getJSONArray("userList"); for(int i=0;i<arry.length();i++){ JSONObject jsonObject5=(JSONObject)arry.get(i); String name=jsonObject5.getString("name"); String cgCount=jsonObject5.getString("cgCount"); User user = new User(); user.setName(name); user.setCgCount(Integer.parseInt(cgCount)); userList.add(user); } } } return userList; } public static Map jsonToObj(String jsonStr) throws Exception { JSONObject jsonObject = new JSONObject(jsonStr); String response = jsonObject.getString("response"); System.out.println("====================>response" + response); String json = jsonObject.getString("result"); System.out.println("json=============>" + json); JSONObject jsonObject2 = new JSONObject(json); String status = jsonObject2.getString("status"); System.out.println("stauts------------>" +status); String name = ""; String uid = ""; String gold = ""; if(jsonStr.indexOf("user") > -1){ String userJson = jsonObject2.getString("user"); if(null !=userJson){ JSONObject jsonObject3 = new JSONObject(userJson); name = jsonObject3.getString("name"); uid = jsonObject3.getString("id"); gold = jsonObject3.getString("gold"); System.out.println("name===>" + name); } } HashMap map = new HashMap(); map.put("response", response); map.put("status", status); map.put("userName", name); map.put("uid", uid); map.put("gold", gold); return map; // JSONArray result=jsonObject.getJSONArray("result"); // // JSONArray data=result.getJSONArray(0); // int length = result.length(); // for (int i = 0; i < length; i++) { // jsonObject = result.getJSONObject(1); // String childName = jsonObject.getString("Name"); // } } public static Map jsonQuestionToObj(String jsonStr) throws Exception { JSONObject jsonObject = new JSONObject(jsonStr); String response = jsonObject.getString("response"); String json = jsonObject.getString("result"); JSONObject jsonObject2 = new JSONObject(json); String answer = null; String gold = null; String uid = ""; String qid = ""; String cgCount = ""; String name = ""; String tips = ""; String uqid = ""; String isQuestion = jsonObject2.getString("isQuestion"); if(isQuestion.equalsIgnoreCase("true")){ System.out.println("true------------------------->>>" + isQuestion); if(jsonStr.indexOf("question") > -1){ String questionJson = jsonObject2.getString("question"); if(null != questionJson){ JSONObject jsonObject4 = new JSONObject(questionJson); answer = jsonObject4.getString("answer"); tips = jsonObject4.getString("tips"); qid = jsonObject4.getString("id"); System.out.println("answer----------------------->" + answer); } } } if(jsonStr.indexOf("user") > -1){ String userJson = jsonObject2.getString("user"); if(null !=userJson){ JSONObject jsonObject3 = new JSONObject(userJson); uid = jsonObject3.getString("id"); name = jsonObject3.getString("name"); gold = jsonObject3.getString("gold"); cgCount = jsonObject3.getString("cgCount"); System.out.println("name===>" + name); } } if(jsonStr.indexOf("userQuestion") > -1){ String userQuestionJson = jsonObject2.getString("userQuestion"); if(null !=userQuestionJson){ JSONObject jsonObject3 = new JSONObject(userQuestionJson); uqid = jsonObject3.getString("qid"); System.out.println("name===>" + uqid); } } HashMap map = new HashMap(); map.put("isQuestion", isQuestion); map.put("answer", answer); map.put("qid", qid); map.put("tips", tips); map.put("response", response); map.put("uid", uid); map.put("name", name); map.put("cgCount", cgCount); map.put("gold", gold); map.put("uqid", uqid); return map; }