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

问题描述 投票:1回答:4

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

我的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();
        }

    }

JSON parser.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;

    }
}
android json android-json
4个回答
3
投票

试着用

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);

1
投票

这是因为你的回复中没有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:没有用户的值


1
投票

你的回答中没有得到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在您的回复中可用,它将获取值


1
投票

如果有可能丢失标签,则不使用'getJSONMethods'使用'optJSONMethods'。

'optMethods'不会抛出异常。它根据类型返回一些默认值。 NSON表示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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.