读书人

濡?ㄤ腑搴

发布时间: 2013-04-02 12:35:26 作者: rapoo

濡?ㄤ腑搴JSONAobject杩JSONArray?

棰浜SDN绛澶В妗?a href="http://ask.csdn.net/questions/1855">http://ask.csdn.net/questions/1855

棰杩板Q?/strong>

涓?釜锛璁よ?JSONArray褰浜璇跺Q杩JSONObject唬?瑙fjsonユ?涓唬ヨВ璇锛

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涓涓SONArraySONArray
涓杞㈡?JSONOBject@浣‘璁?jsonResponse 浼ョ值?JSONArray 杩 JSONObject ?

瑙eQ?/strong>

棣杩袱涓绫诲{G瀛覆哄?/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;}


?互浣n藉璇姝e?[寮?ご
跺杩涓姝?濡寮?ごSONObject json = new JSONObject(jsonResponse);
A杩娉氨inally涓澶?/span>
?涓?涓崇SONObject涓SONArray浠ヤ{?/span>
(toJSONObject/toJSONArray)