How to force Java JSON or GSON to explicitly indicate my arrays and objects

I have the following 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 List<List<Widget>> to JSON using the Java JSON library (however, I also accept any responses as well using 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); 

Fingerprints:

 [ [ { "fizz": "Yes", "buzz": "Never", "foo": "Always" }, { "fizz": "Sometimes", "buzz": "Always", "foo": "Pending" } ] ] 

While I want JSON to display as:

 "widgetGroups": [ "widgetGroup": [ "widget": { "fizz": "Yes", "buzz": "Never", "foo": "Always" }, "widget": { "fizz": "Sometimes", "buzz": "Always", "foo": "Pending" } ] ] 

In other words, I want all lists, as well as every widget , to be "named." My first concern, however, is that this may not be the correct JSON. When I insert this 2nd (desired) JSON snippet in jsonlint.org , I get a parser error.

So first, I ask if anyone can be so kind as to indicate what my desired JSON should be in order to be correct; and then the second , if someone can help me massage my widgetGroups list widgetGroups that either Java JSON or GSON can produce the desired result. Thanks in advance!

+4
source share
2 answers

First, are you sure this is correct? Comment and code do not match.

 // Returns a single List<Widget> with 2 Widgets in it... List<List<Widget>> widgetGroups = getWidgetGroups(); 

Secondly, create a WidgetGroup class that will act as a container for one 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 will be a valid JSON structure:

 { "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.

+5
source

In JSON, there are only two types of “containers” - arrays that are implicitly ordered but not named, and objects (or associative arrays) that are not necessarily ordered but have key value pairs. The correct JSON syntax should be:

 { "widgetGroups": [ { "widgetGroup": [ { "fizz": "Yes", "buzz": "Never", "foo": "Always" }, { "fizz": "Sometimes", "buzz": "Always", "foo": "Pending" } ] } ] } 

It should be noted that inside the "widgetGroup" list I placed an array - you either have to abandon the names, or expose a simple array (syntax [] ), or an object ( {} ) and use unique names (that is, you cannot have two identical items with the name "widget")

As for the syntax for getting this from GSON (or other common java-json transformers), use maps instead of lists where you want to create an associative array.

Change I have not worked with JSON-Java before, but looking at the specifications, it seems you need the JSONObject class to insert associative arrays.

0
source

All Articles