如何用gson反序列化这个json字符串?

时间:2022-11-24 22:18:55

I am trying to deserialize this JSON string in an Android project but I have no experience in it whatsoever.

我试图在Android项目中反序列化这个JSON字符串,但我没有任何经验。

{"nodes":[{"node":{"title":"esesese", "body":"hey world whatup"}}, {"node":{"title":"Asdasd", "body":"asdefasdefe"}}]}

I was trying to do something like this, but it won't work:

我试图做这样的事情,但它不会起作用:

public class Nodes {    
    public Node[] nodes;
}

public class Node {
    public String title;
    public String body;
}

With this code:

使用此代码:

    Nodes articles = new Gson().fromJson(result, Nodes.class);

2 个解决方案

#1


The following works:

以下作品:

public static class Nodes {
    private List<NodeWrapper> nodes;

    public List<NodeWrapper> getNodes() {
        return nodes;
    }

    public void setNodes(List<NodeWrapper> nodes) {
        this.nodes = nodes;
    }
}

public static class Node {
    private String title;
    private String body;

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTitle() { return title; }

    public void setBody(String body) { this.body = body; }

    public String getBody() { return body; }
}


public static class NodeWrapper {
    private Node node;

    public Node getNode() { return node; }

    public void setNode(Node node) { this.node = node; }
}

Then do

Nodes nodes = new Gson().fromJson(result, Nodes.class);

I tried it with this:

我试过这个:

    Nodes nodes = new Gson().fromJson("{\"nodes\":[{\"node\":{\"title\":\"esesese\", \"body\":\"hey world whatup\"}}, {\"node\":{\"title\":\"Asdasd\", \"body\":\"asdefasdefe\"}}]}", Nodes.class);
    System.out.println(nodes.getNodes().get(1).getNode().getBody());

The reason why it is so roundabout is if you look at your JSON:

它如此迂回的原因是你看看你的JSON:

{
    "nodes": [
        {
            "node": {
                "title": "esesese",
                "body": "hey world whatup"
            }
        },
        {
            "node": {
                "title": "Asdasd",
                "body": "asdefasdefe"
            }
        }
    ]
}

Then nodes contains a list of objects that have a node property, which have a title and body property - it's not just a list of objects that contain a title and a body. Therefore, you need a wrapper around that.

然后节点包含具有节点属性的对象列表,该属性具有标题和主体属性 - 它不仅仅是包含标题和正文的对象列表。因此,你需要一个包装器。

#2


You can deserialize as

你可以反序列化为

public class Nodes{
private List<Node> nodes = new ArrayList<Node>();
}

public class Node_ {
private String title;
private String body;
}

public class Node {
private Node_ node;
}

In Code try:

在Code中尝试:

Nodes articles = new Gson().fromJson(result, Nodes.class);

#1


The following works:

以下作品:

public static class Nodes {
    private List<NodeWrapper> nodes;

    public List<NodeWrapper> getNodes() {
        return nodes;
    }

    public void setNodes(List<NodeWrapper> nodes) {
        this.nodes = nodes;
    }
}

public static class Node {
    private String title;
    private String body;

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTitle() { return title; }

    public void setBody(String body) { this.body = body; }

    public String getBody() { return body; }
}


public static class NodeWrapper {
    private Node node;

    public Node getNode() { return node; }

    public void setNode(Node node) { this.node = node; }
}

Then do

Nodes nodes = new Gson().fromJson(result, Nodes.class);

I tried it with this:

我试过这个:

    Nodes nodes = new Gson().fromJson("{\"nodes\":[{\"node\":{\"title\":\"esesese\", \"body\":\"hey world whatup\"}}, {\"node\":{\"title\":\"Asdasd\", \"body\":\"asdefasdefe\"}}]}", Nodes.class);
    System.out.println(nodes.getNodes().get(1).getNode().getBody());

The reason why it is so roundabout is if you look at your JSON:

它如此迂回的原因是你看看你的JSON:

{
    "nodes": [
        {
            "node": {
                "title": "esesese",
                "body": "hey world whatup"
            }
        },
        {
            "node": {
                "title": "Asdasd",
                "body": "asdefasdefe"
            }
        }
    ]
}

Then nodes contains a list of objects that have a node property, which have a title and body property - it's not just a list of objects that contain a title and a body. Therefore, you need a wrapper around that.

然后节点包含具有节点属性的对象列表,该属性具有标题和主体属性 - 它不仅仅是包含标题和正文的对象列表。因此,你需要一个包装器。

#2


You can deserialize as

你可以反序列化为

public class Nodes{
private List<Node> nodes = new ArrayList<Node>();
}

public class Node_ {
private String title;
private String body;
}

public class Node {
private Node_ node;
}

In Code try:

在Code中尝试:

Nodes articles = new Gson().fromJson(result, Nodes.class);