[英]Android json parsing JSONEXCEPTION
我從服務器獲得了responseString,但是當我放入JSONObject時,出現錯誤:JSONException
我的Json String在下面(我是從服務器那里得到的!)
[
{
"myregID": "APA91bHjhw8w_wo6eQOyhrtgx0w8Uypv-oxck28mRP3nfgmg0DFhRvfzpBNHncea7-YrxFV46-_8WVK2UQDXrk6_qqwtHnYlh63P-jobKfGaBi3khnGZ0q-mTLMmWc5ylnv1IcdVYFFRTQqK6oYjEz8BgP_JcxiJ9A",
"yourregID": "APA91bGuEcXeUwMl5PE74JB2KoVSsrYllQl4M0Pil6nPsTCBbMSngriODDLvpcDBgKY1sYj-4NiW8upgLb1VAPf2sgF5ZhUYg2Usdabg-s6CYRVJJBpDFX4MVvOUJ09pbb1lZsBuA6krXJ9CFSTdRBqyoPQid6zYKg"
}
]
我的android客戶端代碼如下。
JSONObject jsonObject = new JSONObject(json);
tmp_myregID = jsonObject.getString("myregID");
Log.v("3333","1111");
tmp_yourregID = jsonObject.getString("yourregID");
代碼流從JSONObject jsonObject = new JSONObject(json)
停止JSONObject jsonObject = new JSONObject(json)
。
您首先需要獲取數組[...]
然后獲取對象{...}
:
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.