1

integer我在插入时遇到问题JSONObject。我integer在 Web 服务中有一个类型的用户 ID。服务器返回我 json 字符串:

[{"userid":207,
  "name" :"Azan",
  "email":"az@gmail.com",
  "password":"az123456",
  "created_at":"11-Oct-2013",
  "success" : 1
  }] 

问题是我必须解析用户 ID JSONObject,但JSONObject只接受字符串,而不是整数。它给了我这样的错误:

10-10 08:44:21.872: W/System.err(5706): org.json.JSONException: Value 207 at userid of  type java.lang.Integer cannot be converted to JSONObject

的代码JSONParser是:

 @SuppressLint("NewApi")
public class JSONParser {

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


    private static String KEY_SUCCESS = "success";
    private static String KEY_UID = "uid";
    private static String KEY_NAME = "name";
    private static String KEY_EMAIL = "email";
    private static String KEY_PASSWORD = "password";
    private static String KEY_CREATED_AT = "created_at";
    // constructor
    public JSONParser() {

    }


    public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {

        // Making HTTP request
        try {
            // 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();

        } 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();
            Log.e("JSON", json);
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
          jObj  = new JSONArray(json).getJSONObject(0);

        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        // return JSON String
        return jObj;

    }
}

任何建议如何解决这个问题?

4

2 回答 2

0

可能是 JSON 解析器拒绝解析格式错误的 JSON。线

sb.append(line + "n");

在我看来很可疑,我想应该是

sb.append(line + "\n");
于 2013-10-11T09:08:44.503 回答
0

首先,检查@Waldheinz 的答案以修复代码中的错误。

其次,你的第一个位置int没有 a 而不是 a ,因此它不起作用。要创建一个新的并将其添加到它使用JSONObjectJSONArrayJSONObjectintnew JSONObject().append("userid", new JSONArray(json).getInt(0));

最后,你正在做的事情一点ThreadPolicy都不好。确保将其删除并将代码放入单独的线程中以进行生产。

于 2013-10-11T09:06:49.427 回答