获取JSONObject - Android

时间:2014-10-09 12:21:20

标签: android json arrays jsonobject

这是我的JSON:

{
  "id": "11111",
  "title": "title?",
  "url": "www.example.com",
  "fulltext": {
    "page1": "<p>balblabba</p>",
    "page2": "<p>avavaava</p>"
  },
  "related_articles": [
    {
      "id": "123",
      "title": "title",
      "image_name": "www.example.com/image1.png"
    }
  ]
}

我需要把所有内容放在字符串中。我使用此代码:

String data = JSON TEXT!!!;
JSONObject jRealObject = new JSONObject(data);

String  title = jRealObject.getString("title").toString();
String  url = jRealObject.getString("url").toString();
String fulltext = jRealObject.getString("fulltext").toString();

String page1 = ????
String page2 = ????

jArray = jRealObject.getJSONArray("related_articles");
for (int i = 0; i < jArray.length(); i++) {
    jRealObject = jArray.getJSONObject(i);

    String relatedId = jRealObject.getString("id").toString();
    String  relatedTitle = jRealObject.getString("title").toString();
    String  relatedImage = jRealObject.getString("image_name").toString();
}

我得到了一切,但我想分开&#34;全文&#34;分为两个变量。但我不知道怎样才能得到&#34; page1&#34;和&#34;第2页&#34;。

4 个答案:

答案 0 :(得分:3)

在您的回复中&#34;全文&#34;是另一个Jsonobject使用下面的代码来获取其他数据。

JSONObject fulltextobj = jRealObject.getJSONObject("fulltext");

    String page1 = fulltextobj.getString("page1");
    String page2 = fulltextobj.getString("page2");

答案 1 :(得分:1)

JSONObject obj = jRealObject.getJSONObject("fulltext");    
String var1 = obj.getString("page1");    
String var2 = obj.getString("page2");

编辑:你有一个JSONObject嵌套到另一个JSONObject中。 fulltext不是String,确实是JSONObject,因此您必须从父

获取JSONObject

答案 2 :(得分:1)

请尝试这种方式,希望这有助于您解决问题。

try{
   JSONObject jsonObject = new JSONObject("{\"id\":\"11111\",\"title\":\"title?\",\"url\":\"www.example.com\",\"fulltext\":{\"page1\":\"<p>balblabba</p>\",\"page2\":\"<p>avavaava</p>\"},\"related_articles\":[{\"id\":\"123\",\"title\":\"title\",\"image_name\":\"www.example.com/image1.png\"}]}");
   HashMap<String,Object> responseMap = new HashMap<String, Object>();
   ArrayList<HashMap<String,String>> articleList =  new ArrayList<HashMap<String, String>>();

   responseMap.put("id",jsonObject.getString("id"));
   responseMap.put("title",jsonObject.getString("title"));
   responseMap.put("url",jsonObject.getString("url"));
   JSONObject fullTextJsonObject = jsonObject.getJSONObject("fulltext");
   responseMap.put("page1",fullTextJsonObject.getString("page1"));
   responseMap.put("page2",fullTextJsonObject.getString("page2"));

   JSONArray relatedArticleJsonArray = jsonObject.getJSONArray("related_articles");
   for(int i=0;i<relatedArticleJsonArray.length();i++){
       HashMap<String,String> articleRow = new HashMap<String, String>();
       articleRow.put("id", relatedArticleJsonArray.getJSONObject(i).getString("id"));
       articleRow.put("title", relatedArticleJsonArray.getJSONObject(i).getString("title"));
       articleRow.put("image_name", relatedArticleJsonArray.getJSONObject(i).getString("image_name"));
       articleList.add(articleRow);
   }
   responseMap.put("related_articles",articleList);

   System.out.print("id : "+responseMap.get("id").toString());
   System.out.print("title : "+responseMap.get("title").toString());
   System.out.print("url : "+responseMap.get("url").toString());
   System.out.print("page1 : "+responseMap.get("page1").toString());
   System.out.print("page2 : "+responseMap.get("page2").toString());
   ArrayList<HashMap<String,String>> list = (ArrayList<HashMap<String,String>>)responseMap.get("related_articles");
   for (HashMap<String,String> row : list){
        System.out.print("id : "+row.get("id"));
        System.out.print("title : "+row.get("title"));
        System.out.print("image_name : "+row.get("image_name"));
   }
}catch (Throwable e){
   e.printStackTrace();
}

答案 3 :(得分:1)

你再次制作字符串toString没有任何意义。请对您的代码进行一些修改。我还做了您需要的更改。

  String data = JSON TEXT!!!;
  JSONObject jRealObject = new JSONObject(data);

  String  title = jRealObject.getString("title");
  String  url = jRealObject.getString("url");
  String fulltext = jRealObject.getString("fulltext");

  JSONObject obj = jRealObject.getJSONObject("fulltext");

   String strPage1 = obj.getString("page1");
   String strPage2 = obj.getString("page2");

 jArray = jRealObject.getJSONArray("related_articles");
 for (int i = 0; i < jArray.length(); i++) {
   jRealObject = jArray.getJSONObject(i);

   String relatedId = jRealObject.getString("id");
   String  relatedTitle = jRealObject.getString("title");
   String  relatedImage = jRealObject.getString("image_name");
}