无法显示 JSON 数组,JSON异常:无值错误

Cant display JSON array , JSONException: No value Error

我正在尝试解析 JSON,我能够通过 Log.e("JSON Object", String.valueOf(json)); 收到响应 {"status":false,"code":"101","message":"Cannot find a POST request in register"},之后我得到一个 JSONException: No value for user。请任何人帮我解决这个问题。 我已经检查了来自互联网的教程和 Whosebug 中的其他问题。但是我还是无法解决我的问题。

我的JSON回复

{"status":false,"code":"101","message":"Cannot find a POST request in register"}

LogCat 错误

W/System.err: org.json.JSONException: No value for user   

我的JSON响应解析码:

       //URL to get JSON Array
       private static String url = "http://mywebsite/api/index.php?action=user_register";

        //JSON Node Names
        private static final String TAG_USER = "user";
        private static final String TAG_ID = "status";
        private static final String TAG_NAME = "code";
        private static final String TAG_EMAIL = "message";
        JSONArray user = null;

    // Creating new JSON Parser
        JSONParser jParser = new JSONParser();

        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl(url);

 try {
            // Getting JSON Array
            user = json.getJSONArray(TAG_USER);
            JSONObject c = user.getJSONObject(0);

            // Storing  JSON item in a Variable
            String id = c.getString(TAG_ID);
            String name = c.getString(TAG_NAME);
            String email = c.getString(TAG_EMAIL);

            Toast.makeText(getContext(), id , Toast.LENGTH_SHORT).show();
            Toast.makeText(getContext(), name , Toast.LENGTH_SHORT).show();
            Toast.makeText(getContext(), email , Toast.LENGTH_SHORT).show();
    } catch (JSONException e) {
            e.printStackTrace();
        }

    }

JSONParser.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}

这是因为您的回复中没有 user 标签,而您正试图获取相同的标签。

您的回复:

{"status":false,"code":"101","message":"Cannot find a POST request in register"}

你正在做:user = json.getJSONArray(TAG_USER);,这里 user 标签丢失,引发错误。

W/System.err: org.json.JSONException: No value for user

尝试使用

if(json.has(TAG_USER))
   user = json.getJSONArray(TAG_USER);

所以如果没有这样的标签你就不会得到错误

更新:

//Storing  JSON item in a Variable
String id = json.getString(TAG_ID);
String name = json.getString(TAG_NAME);
String email = json.getString(TAG_EMAIL);

您的回复中没有 user

{"status":false,"code":"101","message":"Cannot find a POST request in register"}

这就是您获得 JSONException: No value Error

的原因

试试这个

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

   // Storing  JSON item in a Variable
   String id = c.getString(TAG_ID);
   String name = c.getString(TAG_NAME);
   String email = c.getString(TAG_EMAIL);


}

它将获取 user 的值(如果它在您的响应中可用)

而不是使用 'getJSONMethods' 如果有可能丢失标签,请使用 'optJSONMethods'

'optMethods' 不要抛出异常。它 returns 一些默认值取决于类型。 JSONArray 类型为空。

对于您的情况,您可以使用:

user = json.optJSONArray(TAG_USER);

if(null != user) {
    JSONObject c = user.optJSONObject(0);

    // Storing  JSON item in a Variable
    if(null != c) {
        String id = c.optString(TAG_ID);
        String name = c.optString(TAG_NAME);
        String email = c.optString(TAG_EMAIL);
    }
}