有人可以帮助我为什么我的 json 无效,即使我使用 JSON Editor Online 进行检查并且它说我的 json 是有效的?这是我的 json 回复:
08-31 08:02:43.921: W/System.err(4767): org.json.JSONException: Value {"ContactID":5,"AdminUnitID":0,"UserRoleID":0,"SecretQuestionID":0,"FirstName":"nhan2","LastName":"nguyen2","Title":"student","Email":"nhan@mail.com","ProfileURL":"test","InactiveDate":"\/Date(-62135568000000)\/","UserName":null,"Password":null,"SecretAnswer":null,"RegisterDate":"\/Date(-62135568000000)\/","LastLogin":"\/Date(-62135568000000)\/"} of type java.lang.String cannot be converted to JSONObject
这是我的代码:
LoginContact contact = null;
@Override
protected LoginContact doInBackground(String... params) {
JSONObject jObject = null;
try
{
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(params[0]);
request.setHeader("Accept","application/json");
request.setHeader("Content-type", "application/json; charset=utf-8");
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
InputStream in = entity.getContent();
InputStreamReader reader = new InputStreamReader(in, "UTF-8");
try
{
BufferedReader br = new BufferedReader(reader);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = br.readLine()) != null)
{
sb.append(line + "\n");
}
in.close();
String result = sb.toString();
jObject = new JSONObject(result);
contact = new LoginContact(jObject);
}
catch(IOException e)
{
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return contact;
}
这是在我的 .NET Web 服务中返回 Json 的代码:
List<Contacts> list = this.getAllContacts();
JavaScriptSerializer js = new JavaScriptSerializer();
string Json = js.Serialize(list);
return Json;