我该如何解析以下内容?

时间:2012-10-02 12:45:26

标签: android json

我收到了这种格式的回复 (整体回应)

到目前为止,我已尝试过以下代码,但收到错误

private String connect(String url) {

    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);
    HttpResponse response;
    String returnString = null;
    try {
        response = httpclient.execute(httpget);
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            if (entity != null) {

                InputStream instream = entity.getContent();

                String res = convertStreamToString(instream);

                JSONObject jsonObj = new JSONObject(res);

                String f = jsonObj.getString("Result");

                f = f.trim();

                System.out.println("!!!!!!!!!!!!!!! "+f);

                String s= jsonObj.getString("About"); 
                System.out.println("@@@@@@ "+s); 

                JSONArray get = jsonObj.getJSONArray("Result");



                // lets loop through the JSONArray and get all the items 
                for (int i = 0; i < get.length(); i++) { 
                    // printing the values to the logcat 
                    System.out.println("&&&&&&&&&&"+get.getJSONObject(i).getString("About").toString()); 
                    System.out.println("&&&&&&&&&&"+get.getJSONObject(i).getString("AboutMeText").toString());  
                } 



                instream.close();
            }
        } else {
            returnString = "Unable to load page - "
                    + response.getStatusLine();
        }
    } catch (IOException ex) {
        returnString = "Connection failed; " + ex.getMessage();
    } catch (JSONException ex) {
        returnString = "JSON failed; " + ex.getMessage();
    }
    return returnString;
}

private static String convertStreamToString(InputStream is) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

每次我试图解析它,它给了我json失败的异常,并说没有价值.. 如果我在这里犯了任何错误,请告诉我。

2 个答案:

答案 0 :(得分:1)

JSON无效你想要这个吗?

{
"Result": [
    {
        "About": "",
        "AboutMeText": {}
    }
]
}

答案 1 :(得分:0)

更简单的方法是使用GSON。 (来自内存的所有代码......)

for {Result: ["{"About":"","AboutMeText":[{}]]}我创建了一个类

public class Result {
 private String About;
 private String AboutMeText;
  //getters and setters
}

然后为了反序列化

Gson gson = new Gson();
Result result = gson.fromJson(jsonString, Result.class);