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;
}
}
任何建议如何解决这个问题?