I have the following POJO:
我有以下POJO:
public class Widget {
private String fizz;
private String buzz;
private String foo;
// Getters and setters for all 3...
}
In my code, I am trying to convert a List<List<Widget>>
into JSON using the Java JSON library (however I'd also accept any answer using GSON as well).
在我的代码中,我试图使用Java JSON库将List
>转换为JSON(但是我也接受使用GSON的任何答案)。
Here is my code:
这是我的代码:
// Returns a single List<Widget> with 2 Widgets in it...
List<List<Widget>> widgetGroups = getWidgetGroups();
String widgetGroupsAsJson = JSON.encode(widgetGroups);
System.out.println(widgetGroupsAsJson);
This prints:
[
[
{
"fizz": "Yes",
"buzz": "Never",
"foo": "Always"
},
{
"fizz": "Sometimes",
"buzz": "Always",
"foo": "Pending"
}
]
]
Whereas I want the JSON to appear as:
而我希望JSON显示为:
"widgetGroups": [
"widgetGroup": [
"widget": {
"fizz": "Yes",
"buzz": "Never",
"foo": "Always"
},
"widget": {
"fizz": "Sometimes",
"buzz": "Always",
"foo": "Pending"
}
]
]
In other words, I want all the lists, as well as each widget
, to be "named". My first concern, however, is that this may not be proper JSON. When I paste this 2nd (desired) JSON snippet into jsonlint.org I get a parser error.
换句话说,我希望所有列表以及每个小部件都被“命名”。然而,我首先担心的是,这可能不是合适的JSON。当我将第二个(所需的)JSON片段粘贴到jsonlint.org时,我得到一个解析器错误。
So first I'm asking if someone could be so kind as to point out what my desired JSON should look like in order to be proper; and then second if someone could help me massage my widgetGroups
list so that either Java JSON or GSON can produce the desired output. Thanks in advance!
所以首先我要问的是,有人可以如此善良地指出我想要的JSON应该是什么样才能做到正确;然后第二个,如果有人可以帮助我按摩我的widgetGroups列表,以便Java JSON或GSON可以产生所需的输出。提前致谢!
2 个解决方案
#1
5
First, are you sure this is correct? The comment and code does not match .
首先,你确定这是正确的吗?评论和代码不匹配。
// Returns a single List<Widget> with 2 Widgets in it...
List<List<Widget>> widgetGroups = getWidgetGroups();
Second, create a WidgetGroup class that will act as a container for a single WidgetGroup.
其次,创建一个WidgetGroup类,它将充当单个WidgetGroup的容器。
public class WidgetGroup {
private String name;
private List<Widget> widgets;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Widget> getWidgets() {
return widgets;
}
public void setWidgets(List<Widget> widgets) {
this.widgets = widgets;
}
}
This would be a valid JSON structure:
这将是一个有效的JSON结构:
{
"widgetGroups" : [
{
"widgetGroup": [
"widget": {
"fizz": "Yes",
"buzz": "Never",
"foo": "Always"
},
/*More widgets*/
]
},
/*More widget groups*/
]
}
Something like this should work:
像这样的东西应该工作:
Map<String, List<WidgetGroup>> widgetGroups = new HashMap<String, List<WidgetGroup>>();
WidgetGroup widgetGroup1 = getWidgetGroup(); // Just an assumption of one of your methods.
WidgetGroup widgetGroup2 = getWidgetGroup(); // Just an assumption of one of your methods.
List<WidgetGroup> widgetGroupList = new ArrayList<WidgetGroup>();
widgetGroupList.add(widgetGroup1);
widgetGroupList.add(widgetGroup2);
widgetGroups.put("widgetGroups", widgetGroupList);
Then you call toJson()
on the map.
然后在地图上调用json()。
#2
0
In JSON there are only two types of "containers" - arrays, which are implicitly ordered but not named, and object (or associative arrays), which are not necessarily ordered but have named key-value pairs. The correct JSON syntax should be:
在JSON中,只有两种类型的“容器” - 隐式排序但未命名的数组,以及对象(或关联数组),它们不一定是有序但具有命名键值对。正确的JSON语法应该是:
{
"widgetGroups": [
{
"widgetGroup": [
{
"fizz": "Yes",
"buzz": "Never",
"foo": "Always"
},
{
"fizz": "Sometimes",
"buzz": "Always",
"foo": "Pending"
}
]
}
]
}
You should note that inside the "widgetGroup" list i placed an array - you either must give up names and put up a simple array ([] syntax), or an object ({} syntax) and use unique names (i.e. you can't have two identical elements named "widget")
您应该注意,在“widgetGroup”列表中我放置了一个数组 - 您必须放弃名称并放置一个简单的数组([]语法)或一个对象({}语法)并使用唯一的名称(即您可以'有两个相同的元素名为“小部件”)
As for the syntax to get this from GSON (or other common java-json transformers) use maps instead of lists where you want an associative array.
至于从GSON(或其他常见的java-json变换器)获取此语法的语法,请使用map而不是您想要关联数组的列表。
Edit: I've not worked previously with JSON-Java but looking at the specs it seems you need the JSONObject class to insert associative arrays.
编辑:我之前没有使用JSON-Java,但是看看规范,你似乎需要JSONObject类来插入关联数组。
#1
5
First, are you sure this is correct? The comment and code does not match .
首先,你确定这是正确的吗?评论和代码不匹配。
// Returns a single List<Widget> with 2 Widgets in it...
List<List<Widget>> widgetGroups = getWidgetGroups();
Second, create a WidgetGroup class that will act as a container for a single WidgetGroup.
其次,创建一个WidgetGroup类,它将充当单个WidgetGroup的容器。
public class WidgetGroup {
private String name;
private List<Widget> widgets;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Widget> getWidgets() {
return widgets;
}
public void setWidgets(List<Widget> widgets) {
this.widgets = widgets;
}
}
This would be a valid JSON structure:
这将是一个有效的JSON结构:
{
"widgetGroups" : [
{
"widgetGroup": [
"widget": {
"fizz": "Yes",
"buzz": "Never",
"foo": "Always"
},
/*More widgets*/
]
},
/*More widget groups*/
]
}
Something like this should work:
像这样的东西应该工作:
Map<String, List<WidgetGroup>> widgetGroups = new HashMap<String, List<WidgetGroup>>();
WidgetGroup widgetGroup1 = getWidgetGroup(); // Just an assumption of one of your methods.
WidgetGroup widgetGroup2 = getWidgetGroup(); // Just an assumption of one of your methods.
List<WidgetGroup> widgetGroupList = new ArrayList<WidgetGroup>();
widgetGroupList.add(widgetGroup1);
widgetGroupList.add(widgetGroup2);
widgetGroups.put("widgetGroups", widgetGroupList);
Then you call toJson()
on the map.
然后在地图上调用json()。
#2
0
In JSON there are only two types of "containers" - arrays, which are implicitly ordered but not named, and object (or associative arrays), which are not necessarily ordered but have named key-value pairs. The correct JSON syntax should be:
在JSON中,只有两种类型的“容器” - 隐式排序但未命名的数组,以及对象(或关联数组),它们不一定是有序但具有命名键值对。正确的JSON语法应该是:
{
"widgetGroups": [
{
"widgetGroup": [
{
"fizz": "Yes",
"buzz": "Never",
"foo": "Always"
},
{
"fizz": "Sometimes",
"buzz": "Always",
"foo": "Pending"
}
]
}
]
}
You should note that inside the "widgetGroup" list i placed an array - you either must give up names and put up a simple array ([] syntax), or an object ({} syntax) and use unique names (i.e. you can't have two identical elements named "widget")
您应该注意,在“widgetGroup”列表中我放置了一个数组 - 您必须放弃名称并放置一个简单的数组([]语法)或一个对象({}语法)并使用唯一的名称(即您可以'有两个相同的元素名为“小部件”)
As for the syntax to get this from GSON (or other common java-json transformers) use maps instead of lists where you want an associative array.
至于从GSON(或其他常见的java-json变换器)获取此语法的语法,请使用map而不是您想要关联数组的列表。
Edit: I've not worked previously with JSON-Java but looking at the specs it seems you need the JSONObject class to insert associative arrays.
编辑:我之前没有使用JSON-Java,但是看看规范,你似乎需要JSONObject类来插入关联数组。