android - 如何分析下列内容?

  显示原文与译文双语对照的内容
0 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失败的异常,并表示没有值。 请让我知道在这里是否有错误。

时间:原作者:7个回答

0 0

JSON无效你想要这个?

{
"Result": [
 {
"About":"",
"AboutMeText": {}
 }
]
}
原作者:
0 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);
原作者:
...