有人可以帮助我为什么我的json无效,即使我使用JSON编辑器在线检查并且它说我的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;
答案 0 :(得分:1)
你应该解析字符串
Object object = jsonParser.parse(yourstring);
jsonObject = (JSONObject) object;
答案 1 :(得分:1)
你应该这样使用:
JSONObject jArray = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
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();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
jArray = new JSONObject(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return jArray;
}
答案 2 :(得分:1)
使用下面的类来解析你的json文件。
public class JSONParser {
public JSONParser() {
}
JSONObject jObj;
String json;
InputStream is = null;
public JSONObject getJsonFromUrl(String url) {
// TODO Auto-generated method stub
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (Exception 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();
System.out.println("Json String : " + json);
} catch (Exception e) {
e.printStackTrace();
}
try {
jObj = new JSONObject(json);
} catch (Exception e) {
e.printStackTrace();
}
return jObj;
}
}
此类的方法是getJsonFromUrl,返回JSONObject。然后通过迭代JSONObject来访问该对象的数据。
现在使用以下代码在主类中。
JSONParser p = new JSONParser();
JSONObject j = p.getJsonFromUrl("Your json Url or paste your json data here");
// Here get your all data one by one from your jsonObject. In you case
String contactID = j.getString("ContactID");
希望它会对你有所帮助。