0

有人可以帮助我为什么我的 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;
4

3 回答 3

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");

希望它会帮助你。

于 2013-08-31T08:53:41.173 回答
1

你应该解析字符串

Object object = jsonParser.parse(yourstring);

jsonObject = (JSONObject) object;
于 2013-08-31T08:09:27.883 回答
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;
}
于 2013-08-31T08:34:28.607 回答