日期:2014-05-16 浏览次数:20538 次
鍘熼棶棰樻潵鑷簬CSDN闂瓟棰戦亾錛屾洿澶氳В鍐蟲柟妗堣錛?a href="http://ask.csdn.net/questions/1855">http://ask.csdn.net/questions/1855
闂鎻忚堪錛?/strong>
紼嬪簭涓殑涓?釜鏈嶅姟鍣紝榛樿杩斿洖涓?簺JSONArray銆備絾褰撲竴浜涢敊璇彂鐢熸椂錛屽畠杩斿洖JSONObject閿欒鐨勪唬鐮併?鎴戞兂瑙f瀽json鏉ユ鏌ラ敊璇?涓嬮潰鐨勪唬鐮佺敤鏉ヨВ鏋愰敊璇細
public static boolean checkForError(String jsonResponse) { boolean status = false; try { JSONObject json = new JSONObject(jsonResponse); if (json instanceof JSONObject) { if(json.has("code")){ int code = json.optInt("code"); if(code==99){ status = true; } } } } catch (Exception e) { e.printStackTrace(); } return status ; }
浣嗘槸褰?jsonResponse 鍙互杩愯鏃剁‘鑾峰緱 JSONException銆傚畠鏄竴涓狫SONArray錛孞SONArray
涓嶈兘杞崲鎴?JSONOBject銆傚浣曠‘璁?jsonResponse 浼犺繃鏉ョ殑值鏄?JSONArray 杩樻槸 JSONObject ?
瑙e喅鏂規錛?/strong>
棣栧厛鐪嬩笅杩欎袱涓被鍨嬭漿鎹負瀛楃涓插悗鐨勫尯鍒?/span>
...... JSONObject jo = createJSONObject(); JSONArray ja = new JSONArray(); ja.put(jo); System.out.println("jo is "+jo.toString());//{"sex":"鐢?quot;,"username":"ZhangSam","score":123} System.out.println("ja is "+ja.toString());//[{"sex":"鐢?quot;,"username":"ZhangSam","score":123}] } public static JSONObject createJSONObject(){ JSONObject jsonObject = new JSONObject(); try { jsonObject.put("username", "ZhangSam"); jsonObject.put("sex", "鐢?quot;); jsonObject.put("score", new Integer(123)); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return jsonObject; }
鎵?互浣犺鎯衝啓閮藉兘璇嗗埆鐨勶紝鍙渶姝e垯媯?煡鏄惁[
寮?ご鍗衝彲
鐒跺悗鎵嶈兘杩涜浣犵殑杩欎竴姝?濡傛槸闈瀃寮?ご錛孞SONObject json = new JSONObject(jsonResponse);
鍙﹀杩樹竴縐嶆柟娉曞氨鏄湪finally涓鐞?/span>
鏈?悗涓?偣涓庨鏃犲叧鐨勬槸錛孞SONObject涓嶫SONArray鍙互浜掕漿錛?/span>
(toJSONObject/toJSONArray)