使用GSON解析多嵌套json

时间:2016-12-06 15:18:48

标签: java json gson

我必须将json文件解析为文本文件,Sample json文件如下,

{
  "link":"https://xxx.nt",
  "liveChannels":[
                      {
                          "name":"Sony TV",
                          "id":1004,
                          "link":"https://xxx.nt",
                          "decryptionTicket":"https://xxxy.nt",
                          "viewLevel":"Too High",
                              "programs":
                                  {
                                  "totalItems":1,
                                  "programs":[
                                                  {
                                                      "name":"Live or die",
                                                      "id":1000000000,
                                                      "catchUp":["FUN"],
                                                      "startOver":["Again"]
                                                  }
                                              ]
                              }
                  }
              ]
}

我已经使用GSON通过创建以下java类来解析文件。

  1. 频道
  2. LiveChannel
  3. 程序
  4. 子程序
  5. Channel.java

    public class channel 
    {
    
        String link = null;
        ArrayList<liveChannels> liveChannels;
    
        public String getLink() {
            return link;
        }
    
        public void setLink(String link) {
            this.link = link;
        }
    
        public ArrayList<liveChannels> getliveChannels() {
            return liveChannels;
        }
    
        public void setliveChannels(ArrayList<liveChannels> liveChannels) {
            this.liveChannels = liveChannels;
        }
    
    }
    

    livechannel.java

    public class liveChannels {
    
        String name = null;
        int id;
        String link = null;
        String decryptionTicket = null;
        String viewLevel = null;
    
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getLink() {
            return link;
        }
    
        public void setLink(String link) {
            this.link = link;
        }
    
        public String getDecryptionTicket() {
            return decryptionTicket;
        }
    
        public void setDecryptionTicket(String decryptionTicket) {
            this.decryptionTicket = decryptionTicket;
        }
    
        public String getViewLevel() {
            return viewLevel;
        }
    
        public void setViewLevel(String viewLevel) {
            this.viewLevel = viewLevel;
        }
    
    }
    

    之后如何从程序开始解析逻辑。

      

    “程序”:                                     {                                     “TOTALITEMS”:1,

    program.java

    public class programs {
    
        ArrayList<sub_programs> sub_programs;
    
        int totalItems;
    
        public int getTotalItems() {
            return totalItems;
        }
    
        public void setTotalItems(int totalItems) {
            this.totalItems = totalItems;
        }
    
        public ArrayList<sub_programs> getProgramsDetails() {
            return sub_programs;
        }
    
        public void setProgramsDetails(ArrayList<sub_programs> sub_programs) {
            this.sub_programs = sub_programs;
        }
    
    }
    

    sub_program.java

    public class sub_programs {
    
        String name = null;
        int id;
        String catchUp = null;
        String startOver = null;
    
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getCatchUp() {
            return catchUp;
        }
    
        public void setCatchUp(String catchUp) {
            this.catchUp = catchUp;
        }
    
        public String getStartOver() {
            return startOver;
        }
    
        public void setStartOver(String startOver) {
            this.startOver = startOver;
        }
    
    }
    

    主要看起来如下,

        public static void main(String[] args) throws IOException 
        {
    
            Gson gson = new Gson();
    
            String contents = FileUtils.readFileToString(
                    new File("C:/sample.json"), "UTF-8");
    
            channel channelHeader = gson.fromJson(contents, channel.class);
    
            System.out.println("Channel Information --->");
    
            System.out.println("Channel Link: " + channelHeader.getLink());
    
            ArrayList<liveChannels> liveChannels = channelHeader.getliveChannels();
    
            for (int i = 0; i < liveChannels.size(); i++) {
                System.out.println("liveChannels Detail --->");
                liveChannels liveChannelsDetail = liveChannels.get(i);
                System.out.println("Channel Name : " + liveChannelsDetail.getName());
                System.out.println("Channel ID : " + liveChannelsDetail.getId());
                System.out.println("Channel Description Ticket: " + liveChannelsDetail.getDecryptionTicket());
                System.out.println("Channel View Level : " + liveChannelsDetail.getViewLevel());
            }
    
        }
    
    }
    

    任何人都可以帮助获得从livechannel类开始解析程序的逻辑。

    由于程序不是数组列表,还有什么方法可以获得值。

1 个答案:

答案 0 :(得分:1)

您遗失了shutil.copy班级中的orig_file = "first.dat" copy_file = "second.dat" with open(orig_file, "rb") as f1: with open(copy_file, "wb") as f2: # Copy byte by byte byte = f1.read(1) while byte != "": f2.write(byte) byte = f1.read(1) 个对象。

programs

然后在liveChannels课程中,您需要将public class liveChannels { String name = null; int id; String link = null; String decryptionTicket = null; String viewLevel = null; programs programs; public void setPrograms (programs programs) { this.programs = programs; } public programs getPrograms() { return programs; } ... } 字段重命名为programs

sub_programs

顺便说一下,你的类命名不符合Java标准,被认为是不好的做法。你的课程应该这样命名:

programs

请注意,这不会影响GSON解析文档的能力,因为GSON更关心属性名称而不是字段的实际类名。