0

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

到目前为止,我已经尝试了以下代码,但出现错误

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 failed 异常,并表示没有任何价值。如果我在这里犯了任何错误,请告诉我。

4

2 回答 2

1

JSON 无效你想要这个吗?

{
"Result": [
    {
        "About": "",
        "AboutMeText": {}
    }
]
}
于 2012-10-02T12:59:32.060 回答
0

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

因为{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);
于 2012-10-02T13:04:29.187 回答