2016-05-31 111 views
1

我想解析下面给出的json。解析JSON文件

{ 
    "sections": [ 
    { 
     "title": "Title android", 
     "level": 1, 
     "content": [ 
     { 
      "type": "paragraph", 
      "text": "This is paragraph 1 for android." 
     } 
     { 
      "type": "paragraph", 
      "text": "This is paragraph 2 for android" 
     } 
     ], 
     "images": [ 
     { 
      "src": "http://image1 android.", 
      "caption": "Image 1." 
     }, 
     { 
      "src": "http://image2 android", 
      "caption": "Image 2." 
     } 
     ] 
    }, 
    { 
     "title": "Title java", 
     "level": 2, 
     "content": [ 
     { 
      "type": "paragraph", 
      "text": "This is paragraph 1 for Java." 
     }, 
     { 
      "type": "paragraph", 
      "text": "This is paragraph 2 for Java" 
     } 
     ], 
     "images": [ 
     { 
      "src": "http://image1 java.", 
      "caption": "Image 1." 
     }, 
     { 
      "src": "http://image2 java", 
      "caption": "Image 2." 
     } 
     ] 
    }, 
    { 
     "title": "Title json", 
     "level": 3, 
     "content": [ 
     { 
      "type": "paragraph", 
      "text": "This is paragraph 1 for Json." 
     }, 
     { 
      "type": "paragraph", 
      "text": "This is paragraph 2 for Json" 
     }, 
     { 
      "type": "paragraph", 
      "text": "This is paragraph 3 for Json" 
     } 
     ], 
     "images": [ 
     { 
      "src": "http://image1 Json.", 
      "caption": "Image 1." 
     }, 
     { 
      "src": "http://image2 Json", 
      "caption": "Image 2." 
     } 
     ] 
    } 

我想输出这些JSON作为

Title 1 :Title android. \n 
Content 1:This is paragraph 1 for android. 
     This is paragraph 2 for android. 
Image 1:http:// image1 android. 
Image 2:http:// image2 android. 

Title :Title Java. 
Content:This is paragraph 1 for Java. 
     This is paragraph 2 for Java. 
Image 1:http:// image1 Java. 
Image 2:http:// image2 Java. 

...等等。

我做了什么至今

public class ParseJSON { 
    public static String[] titles; 
    public static String[] contents; 
    public static String[] levels; 

    public static final String JSON_ARRAY = "sections"; 
    public static final String TITLE = "title"; 
    public static final String CONTENT = "content"; 
    public static final String TEXT = "text"; 

    private JSONArray sections = null; 
    private JSONArray content = null; 

    private String json; 

    public ParseJSON(String json) { 
     this.json = json; 
    } 

    protected void parseJSON() { 
     JSONObject jsonObject ; 
     try { 
      jsonObject = new JSONObject(json); 
      sections = jsonObject.getJSONArray(JSON_ARRAY); 

      titles = new String[sections.length()]; 
      levels = new String[sections.length()]; 

      for (int i = 0; i < sections.length(); i++) { 
       titles[i] = sections.getJSONObject(i).getString(TITLE); 

       JSONArray content = sections.getJSONObject(i).getJSONArray(CONTENT); 
       contents = new String[content.length()]; 
       Log.d("MainActivity",contents.toString()); 
       for (int j = 0; j < content.length(); j++) { 

        contents[j] += content.getJSONObject(j).getString(TEXT).toString() + "\n\n"; 
        //Log.d("MainActivity",contents.toString()); 
       } 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

上面的代码是不完整的。 我想打印如上所述的json。 但我没有得到所需的标题部分和段落部分。

当我解析从内容阵列TEXT它给所有段落从JSON组合作为内容[0],内容[1]等。 但我想对于标题内容仅

我认为环部起到一定的作用,但我不知道怎么办。

UPDATE 如果我想输出作为中间的一个alone.ie,

//android title part //not needed 
//The part needed is below one: 
Title :Title Java. 
Content:This is paragraph 1 for Java. 
     This is paragraph 2 for Java. 
Image 1:http:// image1 Java. 
Image 2:http:// image2 Java. 

//json title part //not needed 
+0

强烈考虑让Java类将标题,内容和图像放在一个对象下,而不是单独的数组。改为使用对象类型的数组。 –

+0

我很惊讶没有人提到过Gson? – Eenvincible

+0

@Eenvincible你能用一个简单的例子来帮助我解决同一个问题。谢谢 –

回答

1

使用JSON转换器POJO工具,如this,您可以生成普通Java对象(PO​​JO),您可以用它来轻松地操纵您的JSON结果。

从您的JSON字符串,我能够生成以下Java类:

public class Content { 

    @SerializedName("type") 
    @Expose 
    private String type; 
    @SerializedName("text") 
    @Expose 
    private String text; 

    /** 
    * 
    * @return 
    * The type 
    */ 
    public String getType() { 
     return type; 
    } 

    /** 
    * 
    * @param type 
    * The type 
    */ 
    public void setType(String type) { 
     this.type = type; 
    } 

    /** 
    * 
    * @return 
    * The text 
    */ 
    public String getText() { 
     return text; 
    } 

    /** 
    * 
    * @param text 
    * The text 
    */ 
    public void setText(String text) { 
     this.text = text; 
    } 

} 

那么这代表着图像实体:

public class Image { 

    @SerializedName("src") 
    @Expose 
    private String src; 
    @SerializedName("caption") 
    @Expose 
    private String caption; 

    /** 
    * 
    * @return 
    * The src 
    */ 
    public String getSrc() { 
     return src; 
    } 

    /** 
    * 
    * @param src 
    * The src 
    */ 
    public void setSrc(String src) { 
     this.src = src; 
    } 

    /** 
    * 
    * @return 
    * The caption 
    */ 
    public String getCaption() { 
     return caption; 
    } 

    /** 
    * 
    * @param caption 
    * The caption 
    */ 
    public void setCaption(String caption) { 
     this.caption = caption; 
    } 

} 

这是一个包含两个对象的伞列表:

public class Section { 

    @SerializedName("title") 
    @Expose 
    private String title; 
    @SerializedName("level") 
    @Expose 
    private int level; 
    @SerializedName("content") 
    @Expose 
    private List<Content> content = new ArrayList<Content>(); 
    @SerializedName("images") 
    @Expose 
    private List<Image> images = new ArrayList<Image>(); 

    /** 
    * 
    * @return 
    * The title 
    */ 
    public String getTitle() { 
     return title; 
    } 

    /** 
    * 
    * @param title 
    * The title 
    */ 
    public void setTitle(String title) { 
     this.title = title; 
    } 

    /** 
    * 
    * @return 
    * The level 
    */ 
    public int getLevel() { 
     return level; 
    } 

    /** 
    * 
    * @param level 
    * The level 
    */ 
    public void setLevel(int level) { 
     this.level = level; 
    } 

    /** 
    * 
    * @return 
    * The content 
    */ 
    public List<Content> getContent() { 
     return content; 
    } 

    /** 
    * 
    * @param content 
    * The content 
    */ 
    public void setContent(List<Content> content) { 
     this.content = content; 
    } 

    /** 
    * 
    * @return 
    * The images 
    */ 
    public List<Image> getImages() { 
     return images; 
    } 

    /** 
    * 
    * @param images 
    * The images 
    */ 
    public void setImages(List<Image> images) { 
     this.images = images; 
    } 

} 

在生成它们并像其他任何项目一样保存在项目中之后类,现在可以使用Gson将json字符串转换为具有属性的这些对象。

既然你在你的JSON响应获取部分List,你可以做的很简单:

List<Section> sections = new Gson().fromJson(jsonString, Section.class); 

现在你有节,你可以遍历获得的图像和内容的列表。内容,请记住,有一个自己的列表,就像图像一样。从那里你应该能够轻松获得你的数据。

我希望这可以帮助你。

您可以使用Gradle添加Gson或下载jar文件并将其添加到您在android studio中的/libs文件夹中。

2

你为什么要折磨自己手动解析JSON?我可以推荐你使用一些轻量级的JSON解析器。这是我做的是在Android中使用org.codehaus.jackson映射器:

package yourpackage; 

import java.io.IOException; 
import java.io.StringWriter; 
import java.util.List; 

import org.codehaus.jackson.JsonGenerationException; 
import org.codehaus.jackson.JsonParseException; 
import org.codehaus.jackson.map.JsonMappingException; 
import org.codehaus.jackson.map.ObjectMapper; 

public class JsonMapper 
{ 
    private static ObjectMapper objectMapper = new ObjectMapper(); 

    public static Object fromJsonToJavaObject(String jsonObject, Class<?> clazz) throws JsonParseException, JsonMappingException, IOException 
    { 
     return objectMapper.readValue(jsonObject, clazz); 
    } 

    public static String fromJavaObjectToJson(Object javaObject) throws JsonGenerationException, JsonMappingException, IOException 
    { 
     StringWriter stringWriter = new StringWriter(); 

     objectMapper.writeValue(stringWriter, javaObject); 

     return stringWriter.toString(); 
    } 

    public static List<?> fromJsonToJavaObjects(String jsonObject, Class<?> clazz) throws JsonParseException, JsonMappingException, IOException 
    { 
     return objectMapper.readValue(jsonObject, objectMapper.getTypeFactory().constructCollectionType(List.class, clazz)); 
    } 
} 

对于你的情况,只是通过JSON字符串和期望的结果类型像TitleWithImages.class第一种方法。 这将是Maven的依赖关系(但您使用的是Android工作室):

<dependency> 
    <groupId>org.codehaus.jackson</groupId> 
    <artifactId>jackson-mapper-asl</artifactId> 
    <version>1.9.13</version> 
</dependency> 
+0

或者'new Gson()。fromJson(String,Class)'...完成相同任务的时间要短得多 –

+0

@ cricket_007前段时间,我想我在Android上使用Gson和解析时出现了内存不足的错误问题大量的数据。为什么它比我的提议'objectMapper.readValue(jsonObject,clazz);'? – Bevor

+0

定义对象映射器是另外一行代码:)前段时间我用过Jackson,但我不确定为什么我切换到Gson –

3
try { 
     JSONObject jsonObject = new JSONObject(data); 
     sections = jsonObject.getJSONArray("sections"); 
     for (int i = 0; i < sections.length(); i++) { 
      JSONObject contentJSON = sections.getJSONObject(i); 
      Log.d("", "Title: "+ contentJSON.getString("level") + " " + contentJSON.getString("title")); 
      JSONArray contentArray = contentJSON.getJSONArray("content"); 
      Log.d("","Content: " + contentJSON.getString("level") + " " ); 
      for (int j = 0; j < contentArray.length(); j++) { 
       Log.d("",contentArray.getJSONObject(i).getString("text")); 
      } 
      JSONArray imageArray = contentJSON.getJSONArray("images"); 
      Log.d("","Images: " + contentJSON.getString("level") + " " ); 
      for (int j = 0; j < imageArray.length(); j++) { 
       Log.d("",imageArray.getJSONObject(j).getString("src")); 
      } 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

我已经印在logcat的输出,你可以再补充它在字符串数组或更好参数创建对象,你需要

+0

我改变了不同输出的问题。 您上面给出的答案正在工作。但是,只有我想起了这个特殊的变化。 你能否让答案适用于那一个。 –

+0

只需将您的标题与您想要解析的部分进行比较即可。如果匹配,则解析并存储其余的东西。 'if(contentJSON.getString(“title”)。equals(“Title Java”))' 然后解析休息或继续循环 –