0

我正在尝试使用 android 解析从 php 发送的 json 数据。这是代码。json 输出如下所示:“{documents:[{idnumber:'28044684',doctype:'National ID'}],success:1}”

ERROR parsing JSONObject value [null];即使我直接将json输出分配给变量json,我也会收到错误消息。可能是什么问题呢??

String json = "";
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("idnumber", parameter));
        // Making HTTP request
        try {

            // check for request method
            if (method == "POST") {
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            } else if (method == "GET") {
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);
                HttpResponse httpResponse = httpClient.execute(httpGet);
                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, "utf-8"), 300);
            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 (Exception e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
            try {
                jObj = new JSONObject(json.substring(json.indexOf("{"),
                        json.lastIndexOf("}") + 1));
            } catch (Exception e0) {
                Log.e("JSON Parser0",
                        "Error parsing data [" + e0.getMessage() + "] "
                                + json);
                Log.e("JSON Parser0", "Error parsing data " + e0.toString());
                try {
                    jObj = new JSONObject(json.substring(1));
                } catch (Exception e1) {
                    Log.e("JSON Parser1",
                            "Error parsing data [" + e1.getMessage() + "] "
                                    + json);
                    Log.e("JSON Parser1",
                            "Error parsing data " + e1.toString());
                    try {
                        jObj = new JSONObject(json.substring(2));
                    } catch (Exception e2) {
                        Log.e("JSON Parser2",
                                "Error parsing data [" + e2.getMessage()
                                        + "] " + json);
                        Log.e("JSON Parser2",
                                "Error parsing data " + e2.toString());
                        try {
                            jObj = new JSONObject(json.substring(3));
                        } catch (Exception e3) {
                            Log.e("JSON Parser3", "Error parsing data ["
                                    + e3.getMessage() + "] " + json);
                            Log.e("JSON Parser3", "Error parsing data "
                                    + e3.toString());
                        }
                    }
                }
            }
        }
        return null;
4

2 回答 2

3

JSON的无效

{
documents: [
    {
        idnumber: '28044684',
        doctype: 'NationalID'
    }
],
success: 1
}

请参阅:http: //json.org/example.html获取一些正确的格式

正确的格式应该是

{
"documents": [
    {
        "idnumber": "28044684",
        "doctype": "NationalID"
    }
],
"success": 1
}

在这里验证

于 2013-05-03T10:20:01.277 回答
2

尝试在 catch 之后更改您的 try 块

 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;

然后直接从返回的 JSONObject 中提取你想要的信息

JSONObject json = jsonParser.makeHttpRequest(host + url_app_list,
                    "GET", params);

            if (json == null) {
                Log.e("Server Warning", "No Server Response");
                message = "No Server Response";
            } else {

                Log.d("All Apps: ", json.toString());

                try {

                    success = json.getInt(TAG_SUCCESS);

                    if (success == 1) {

                        apps = json.getJSONArray(TAG_APPS);

                        for (int i = 0; i < apps.length(); i++) {
                            JSONObject c = apps.getJSONObject(i);
                            AppData tempApp = new AppData();

                            tempApp.setName(c.getString(TAG_NAME));
                            // tempApp.setPackageName(c.getString(TAG_PACKAGE));
                            tempApp.setServerVersion(c.getInt(TAG_VERSION));
                            tempApp.setServerDateVersion(c.getInt(TAG_DVERSION))

...ETC。这将使处理更简单,但您的 JSON 无效,字符串应全部双引号,见下文

{"success":0,"message":"Required field(s) missing"}
于 2013-05-03T10:23:16.407 回答