简体   繁体   English

用GSON解析多嵌套json

[英]parsing multi nested json with GSON

I have to parse the json file into text file, Sample json file as below, 我必须将json文件解析为文本文件,示例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"] } ] } } ] } 

I have used GSON to parse the file by creating the below java classes. 我已经使用GSON通过创建以下java类来解析文件。

  1. Channel 渠道
  2. LiveChannel 直播频道
  3. programs 程式
  4. subprograms 子程序

Channel.java 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 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;
    }

}

After this how to parse the logic from program onwards. 之后,如何从程序开始解析逻辑。

"programs": { "totalItems":1, “程序”:{“ 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 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;
    }

}

and main look like below, 主要外观如下

    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());
        }

    }

}

Could anyone please help to get the logic to parse the program from livechannel class onwards. 任何人都可以帮助获得从livechannel类开始解析程序的逻辑。

As programs is not an array list , What else would be an other way around to get the values. 由于程序不是数组列表,因此还有其他方法来获取值。

You are missing the programs object in your liveChannels class. 您缺少liveChannels类中的programs对象。

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;
    }

    ...
}

And then in your programs class, you will need to rename the sub_programs field to programs 然后在programs类中,您需要将sub_programs字段重命名为programs

public class programs {

    ArrayList<sub_programs> programs;

    ...
}

As an aside, your class naming does not follow Java standards and is considered bad practice. 顺便说一句,您的类命名不遵循Java标准,被认为是不好的做法。 Your classes should be named as such: 您的类应这样命名:

Channel
LiveChannel
Program
SubProgram

Note that this will not affect GSON's ability to parse your documents as GSON cares more about the property name than it does the actual class name of the field. 请注意,这不会影响GSON解析文档的能力,因为GSON更关心属性名而不是字段的实际类名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM