如何使用 gson 反序列化此 json 字符串?
How can I deserialize this json string with gson?
我正在尝试在 Android 项目中反序列化此 JSON 字符串,但我对此没有任何经验。
{"nodes":[{"node":{"title":"esesese", "body":"hey world whatup"}}, {"node":{"title":"Asdasd", "body":"asdefasdefe"}}]}
我正在尝试做这样的事情,但行不通:
public class Nodes {
public Node[] nodes;
}
public class Node {
public String title;
public String body;
}
使用此代码:
Nodes articles = new Gson().fromJson(result, Nodes.class);
你可以反序列化为
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;
}
在代码中尝试:
Nodes articles = new Gson().fromJson(result, Nodes.class);
以下作品:
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; }
}
然后做
Nodes nodes = new Gson().fromJson(result, Nodes.class);
我试过这个:
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());
之所以如此迂回,是因为如果你看看你的 JSON:
{
"nodes": [
{
"node": {
"title": "esesese",
"body": "hey world whatup"
}
},
{
"node": {
"title": "Asdasd",
"body": "asdefasdefe"
}
}
]
}
然后 nodes
包含具有 node
属性 的对象列表,其中具有 title
和 body
属性 -它不仅仅是包含 title
和 body
的对象列表。因此,你需要一个包装器。
我正在尝试在 Android 项目中反序列化此 JSON 字符串,但我对此没有任何经验。
{"nodes":[{"node":{"title":"esesese", "body":"hey world whatup"}}, {"node":{"title":"Asdasd", "body":"asdefasdefe"}}]}
我正在尝试做这样的事情,但行不通:
public class Nodes {
public Node[] nodes;
}
public class Node {
public String title;
public String body;
}
使用此代码:
Nodes articles = new Gson().fromJson(result, Nodes.class);
你可以反序列化为
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;
}
在代码中尝试:
Nodes articles = new Gson().fromJson(result, Nodes.class);
以下作品:
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; }
}
然后做
Nodes nodes = new Gson().fromJson(result, Nodes.class);
我试过这个:
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());
之所以如此迂回,是因为如果你看看你的 JSON:
{
"nodes": [
{
"node": {
"title": "esesese",
"body": "hey world whatup"
}
},
{
"node": {
"title": "Asdasd",
"body": "asdefasdefe"
}
}
]
}
然后 nodes
包含具有 node
属性 的对象列表,其中具有 title
和 body
属性 -它不仅仅是包含 title
和 body
的对象列表。因此,你需要一个包装器。