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

时间:2015-12-30 06:19:11

标签: android json android-json

我试图解析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();
        }

    }

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;

    }
}

4 个答案:

答案 0 :(得分: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 :(得分: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:没有用户值

答案 2 :(得分: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,它将获取devise的值

答案 3 :(得分:1)

如果有可能缺少标记,则不使用'getJSONMethods'使用'optJSONMethods'

'optMethods'不会抛出异常。它根据类型返回一些默认值。对于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);
    }
}