java.lang.Integer类型的userid上的值207无法转换为JSONObject

时间:2013-10-11 09:03:12

标签: java android json

integer放入JSONObject时遇到问题。我在Web服务中有一个类型为integer的用户标识。服务器返回json string:

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

问题是我必须解析JSONObject中的userid,但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;

    }
}

有任何建议如何解决问题?

2 个答案:

答案 0 :(得分:0)

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

其次,您int的第一个位置上没有JSONObjectJSONArray,因此无效。要创建新的JSONObject并将int添加到其中,请使用new JSONObject().append("userid", new JSONArray(json).getInt(0));

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

答案 1 :(得分:0)

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

sb.append(line + "n");

对我来说非常可疑,我猜应该是

sb.append(line + "\n");