简体   繁体   English

Android json解析JSONEXCEPTION

[英]Android json parsing JSONEXCEPTION

I got responseString from server, but when I put into JSONObject, there is an error: JSONException 我从服务器获得了responseString,但是当我放入JSONObject时,出现错误:JSONException

My Json String is below (what I exactly got from server!) 我的Json String在下面(我是从服务器那里得到的!)

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


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

And my android Client Code is below about that. 我的android客户端代码如下。

     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) . 代码流从JSONObject jsonObject = new JSONObject(json)停止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。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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