使用Java

时间:2017-11-12 07:49:57

标签: java json

我正在尝试用Java解析JSON文件。但是JSON文件的格式很复杂(至少对我而言),因此我遇到了障碍。

你可以阅读文件 - Reddit JSON file

在“reddit”文件中,我感兴趣的“密钥”是“body”,它嵌套在文件中的JSON数组的深处。

我正在使用Jackson将JSON解析和反序列化为Java对象,我的代码如下 -

class Reddit
{
    private String kind;
    private String data;

    public String getData()
    {
        return this.data;
    }

    public void setData(String data)
    {
        this.data = data;
    }

    public String getKind()
    {
        return this.data;
    }

    public void setKind(String data)
    {
        this.data = data;
    }

}


public class Attempting_Read_JSON
{
    public static void main(String[] args)
    {
        // ObjectMapper can parse JSON into Java Objects, can do vice-versa and more-
        ObjectMapper OM = new ObjectMapper();

        try
        {
            out.println("\nInside try block.\n");
            File json_file = new File("src/main/resources/data/reddit/redditdump.json");
            Reddit[] R = OM.readValue(json_file, Reddit[].class);

            out.println("\nRead from JSON file to 'Reddit class object'\n");

            //out.println("\nObtained JSON- " + R.getData());
        }
        catch (Exception E)
        {
            E.printStackTrace();
        }

    out.println("\n\nProgram terminated successfully!\n");
    }
}

但是在执行程序时,它一直给我错误。 关于如何获得与关键字'身体'相关的价值的任何建议?

谢谢!

1 个答案:

答案 0 :(得分:0)

在您的JSON文件中"data":是一个对象,您正试图通过String分配到Reddit课程中的private String data;

这就是你收到此错误的原因。

您需要使用Data创建一个类members contest_mode, banned_by....或使用JSON对象来解析它。

另外,为什么要设置data这里使用kind

    public String getKind()
    {
        return this.data;
    }

    public void setKind(String data)
    {
        this.data = data;
    }

<强> 编辑: 为简单起见,我将举一个小例子:

JSON文件:

[
    {
        "kind": "Listing",
        "data": {
            "contest_mode": false
        }
    },
    {
        "kind": "Listing",
        "data": {
            "contest_mode": false
        }
    }
]

您的代码:

public class Attempting_Read_JSON {
    public static void main(String[] args) {
        ObjectMapper OM = new ObjectMapper();

        try {
            out.println("\nInside try block.\n");
            File json_file = new File(
                    "src/main/resources/data/reddit/redditdump.json");
            Reddit[] R = OM.readValue(json_file, Reddit[].class);

            out.println("\nRead from JSON file to 'Reddit class object'\n" + R[0].toString());

            // out.println("\nObtained JSON- " + R.getData());
        } catch (Exception E) {
            E.printStackTrace();
        }

        out.println("\n\nProgram terminated successfully!\n");
    }
}

class Reddit {
    private String kind;
    @Override
    public String toString() {
        return "Reddit [kind=" + kind + ", data=" + data.toString() + "]";
    }

    private Data data;

    public Data getData() {
        return this.data;
    }

    public void setData(Data data) {
        this.data = data;
    }

    public String getKind() {
        return this.kind;
    }

    public void setKind(String kind) {
        this.kind = kind;
    }
}

class Data
{
    boolean contest_mode;

    public boolean isContest_mode() {
        return contest_mode;
    }

    public void setContest_mode(boolean contest_mode) {
        this.contest_mode = contest_mode;
    }

    @Override
    public String toString() {
        return "[contest_mode=" + contest_mode + "]";
    }
}