Android 具有 JSON 解析错误的 AsyncTask

Android AsyncTask with JSON Parsing error

此部分出现错误:

protected void onPostExecute(JSONObject json) {
    try {
        // Getting JSON Array
        user = json.getJSONArray(TAG_USER);
        JSONObject c = json.getJSONObject(0);
        String id = c.getString(TAG_ID);
        String name = c.getString(TAG_NAME);
        Log.i("id",id);
    }
}

------ 网络服务结果---

{"GetDataResult":{"ID":8,"Name":"Foo Bar"}} Working Link - http://127.0.0.1/WcfService4/Service1.svc/getData/?key=8

提供更好的解决方案。

看来问题出在这一段:

user = json.getJSONArray(TAG_USER);
JSONObject c = json.getJSONObject(0);

你得到了一个 JOSNObject user 但你从未使用过它。

我想这可能是你的意思:

user = json.getJSONArray(TAG_USER);
JSONObject c = user.getJSONObject(0);

您似乎还没有使用 JSONArray 对象

JSONArray mainfoodlist = null;


    tipshealth = json.getJSONArray(TAG_RESPONSE);

        // looping through All RESPONSE
        for (int i = 0; i < tipshealth.length(); i++) {
        JSONObject jsonobj = tipshealth.getJSONObject(i);
        tipHealth = jsonobj.getString(KEY_HEALTHTIPS);

        listhealthtips.add(tipshealth.getJSONObject(i).getString("tips"));

        }