简体   繁体   中英

Android json parsing JSONEXCEPTION

I got responseString from server, but when I put into JSONObject, there is an error: JSONException

My Json String is below (what I exactly got from server!)

  [
{
    "myregID": "APA91bHjhw8w_wo6eQOyhrtgx0w8Uypv-oxck28mRP3nfgmg0DFhRvfzpBNHncea7-YrxFV46-_8WVK2UQDXrk6_qqwtHnYlh63P-jobKfGaBi3khnGZ0q-mTLMmWc5ylnv1IcdVYFFRTQqK6oYjEz8BgP_JcxiJ9A",


    "yourregID": "APA91bGuEcXeUwMl5PE74JB2KoVSsrYllQl4M0Pil6nPsTCBbMSngriODDLvpcDBgKY1sYj-4NiW8upgLb1VAPf2sgF5ZhUYg2Usdabg-s6CYRVJJBpDFX4MVvOUJ09pbb1lZsBuA6krXJ9CFSTdRBqyoPQid6zYKg"
}
  ]

And my android Client Code is below about that.

     JSONObject jsonObject = new JSONObject(json);
     tmp_myregID = jsonObject.getString("myregID");
     Log.v("3333","1111");
     tmp_yourregID = jsonObject.getString("yourregID");

code flow stops at JSONObject jsonObject = new JSONObject(json) .

You first need to fetch the array [...] then the object {...} :

try {
    JSONArray jsonArray = new JSONArray(jsonString);
    JSONObject jsonObject = jsonArray.getJSONObject(0);
    Log.e(TAG, jsonObject.getString("myregID"));
    Log.e(TAG, jsonObject.getString("yourregID"));
} catch (JSONException e) {
    e.printStackTrace();
}

您的Json根元素是JsonArray而不是Json Object,因此请首先获取JsonArray。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM