I am trying to generate the following json output using the java net.sf.json libs but have been unsuccessful.
[
{
"data": [
[
1,
1,
"Text"
],
[
2,
2,
"Text"
],
[
3,
0,
"Text"
],
[
5,
2,
"Text"
]
],
"label": "First Series"
}
]
I have read on these forums Gson is my best bet going forward. Can anyone provide an example of how to generate this json using Gson or another suitable java based library.
Thanks in advance
i like this
http://www.json.org/javadoc/org/json/JSONObject.html from http://json.org/java/
and JSONArray.
with those 2 objects:
JSONArray inner = new JSONArray()
inner.add(1);inner.add("text");
JSONObject outer = new JSONObject();
outer.put("data",inner);
outer.put("label", "stuff");
String out = outer.toString()
Gson
Gson is a Java library that can be used to convert Java Objects into its JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.
There are a few open-source projects that can convert Java objects to JSON. However, most of them require that you place Java annotations in your classes something that you can not do if you do not have access to the source-code. Most also do not fully support the use of Java Generics. Gson considers both of these as very important design goals.
import com.google.gson.Gson;
class Person {
private int age = 10;
private String name = "jigar";
}
Person obj = new Person();
Gson gson = new Gson();
String json = gson.toJson(obj);
http://json.org/java/
import org.json.JSONObject;
...
...
JSONObject json = new JSONObject();
json.put("city", "Mumbai");
json.put("country", "India");
...
String output = json.toString();
This is easy enough using a Java object like this:
public class GsonTest {
private List<DataItem> data;
private String label;
public GsonTest() {} // for Gson
public GsonTest(List<DataItem> data, String label) {
this.data = data;
this.label = label;
}
// ...
}
public class DataItem {
private int val1;
private int val2;
private String text;
public DataItem() {} // for Gson
public DataItem(int val1, int val2, String text) {
this.val1 = val1;
this.val2 = val2;
this.text = text;
}
// ...
}
Since your JSON format uses an array rather than an object for each data item (an object would make more sense based on your sample) you need to add a custom handler for serializing and deserializing DataItems to and from JSON arrays.
public class DataItemConverter implements JsonDeserializer<DataItem>,
JsonSerializer<DataItem> {
public DataItem deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
JsonArray array = json.getAsJsonArray();
int val1 = array.get(0).getAsInt();
int val2 = array.get(1).getAsInt();
String text = array.get(2).getAsString();
return new DataItem(val1, val2, text);
}
public JsonElement serialize(DataItem src, Type typeOfSrc,
JsonSerializationContext context) {
JsonArray array = new JsonArray();
array.add(new JsonPrimitive(src.val1));
array.add(new JsonPrimitive(src.val2));
array.add(new JsonPrimitive(src.text));
return array;
}
}
Then you just need to register this converter when you create your Gson instance and you're good to go! Since our DataItem converter handles deserialization as well, you'll be able to deserialize the generated JSON as a List<GsonTest> as well.
public static void testSerialization() {
List<DataItem> data = new ArrayList<DataItem>();
data.add(new DataItem(1, 1, "Text"));
data.add(new DataItem(2, 2, "Text"));
data.add(new DataItem(3, 0, "Text"));
data.add(new DataItem(5, 2, "Text"));
GsonTest test = new GsonTest(data, "First Series");
List<GsonTest> list = new ArrayList<GsonTest>();
list.add(test);
Gson gson = new GsonBuilder()
.registerTypeAdapter(DataItem.class, new DataItemConverter())
.create();
System.out.println(gson.toJson(list));
}
Related
In the Reddit JSON API, comments can contain two different types of JSONArrays, both called "children".
"children" is usually an array of Objects containing a String "kind" and Object "data":
"children": [ { "kind": "t3", "data": {} } ...]
I've been handling these fine. My problem is that, sometimes, children will be a simple String array:
"children": [ "e78i3mq", "e78hees", "e78jq6q" ]
When parsing these, GSON throws an exception like the following:
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but
was STRING at line 1 column 3780 path
$[1].data.children[0].data.replies.data.children[0].data.replies.data.children[0].data.replies.data.children[0].data.children[0]
How can I handle these String array cases?
If the same endpoint is returning a different type in some instances I suggest wrapping that part in an object and using a deserializer to check the type and assign accordingly. You can do something like this:
public Parent serialize(String jsonString) {
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(WrappedChild.class, new ChildDeserializer());
Gson gson = builder.create();
return gson.fromJson(jsonString, Parent.class);
}
class Parent {
public List<WrappedChild> children;
}
class ObjectChild {
public String body;
}
class WrappedChild {
public ObjectChild objectChild;
public String stringChild;
}
class ChildDeserializer implements JsonDeserializer<WrappedChild> {
private Gson gson = new Gson();
#Override
public WrappedChild deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (json != null) {
if (json.isJsonObject()) {
WrappedChild result = new WrappedChild();
result.objectChild = gson.fromJson(json, ObjectChild.class);
return result;
} else if (json.isJsonPrimitive()) {
WrappedChild result = new WrappedChild();
result.stringChild = json.getAsString();
return result;
}
}
return null; // Or throw new Exception("Unknown child type");
}
}
If you are using retrofit just pass the Gson created by the builder to GsonConverterFactory.create as a parameter when creating your service.
You should carefully study answer from Emre Eran because that way you will have a total control of deserializing. I will just give an another approach which in some cases might require less effort. It bases on the "basic intelligence" of Gson deserializing.
If you declare your class that contains children like:
public class Parent {
Collection<?> children;
}
Gson tries its best to "guess" the object type. If it faces a simple string it will be deserialized to a String. If it faces data like in your 1st Json example, it will deserialized to com.google.gson.internal.LinkedTreeMap which is just a Java version of Json tree.
So depending on how complex is the data object in your 1st example and how you use the result overall you might not need to write custom deserializers (which anyway might be better solution eventually).
Sorry for the late answer, thanks for leading me in the right direction Emre!
I ended up getting GsonBuilder to work with a custom method, getGsonAdaptedData.
After retrieving the JSON response in a background thread:
...
Gson gson = new GsonBuilder().registerTypeAdapter(Data.class, (JsonDeserializer<Data>) (arg0, arg1, arg2) -> {
JsonObject dataJsonObject = arg0.getAsJsonObject();
Data data = new Gson().fromJson(dataJsonObject, Data.class);
return RedditUtils.getGsonAdaptedData(dataJsonObject.get("children").getAsJsonArray(), data);
}).create();
final Feed responseSubredditFeed = gson.fromJson(jsonString, Feed.class);
...
RedditUtils.getGsonAdaptedData
// JSON returned for Reddit comments can contain two types of arrays named "children"
// This method checks to see if we were given a Children array or String array
// JSON member "replies" is similar, and can be found in the Data of some Children
// If the method finds a nested "children" array, it recursively adapts its Data
public static Data getGsonAdaptedData(JsonArray childrenJsonArray, Data data) {
if (childrenJsonArray.size() > 0) {
Gson gson = new Gson();
if (childrenJsonArray.get(0).isJsonObject()) {
data.setChildrenList(gson.fromJson(childrenJsonArray,
new TypeToken<List<Children>>() {
}.getType()));
// Loops through every Data object in the array looking for children and replies
for (int i = 0; i < childrenJsonArray.size(); i++) {
JsonObject nestedDataJsonObject = childrenJsonArray.get(i).getAsJsonObject().get("data").getAsJsonObject();
if (nestedDataJsonObject.has("children")) {
getGsonAdaptedData(nestedDataJsonObject.get("children").getAsJsonArray(), data.getChildren().get(i).getData());
} else if (nestedDataJsonObject.has("replies") && nestedDataJsonObject.get("replies").isJsonObject()) {
data.getChildren().get(i).getData().setRepliesObject(gson.fromJson(nestedDataJsonObject.get("replies"),
new TypeToken<Replies>() {
}.getType()));
getGsonAdaptedData(nestedDataJsonObject.get("replies").getAsJsonObject().get("data").getAsJsonObject().get("children").getAsJsonArray(), data.getChildren().get(i).getData());
}
}
} else {
data.setRepliesList(gson.fromJson(childrenJsonArray,
new TypeToken<List<String>>() {
}.getType()));
}
}
return data;
}
I'm trying to deserialize the following structure
{ meta: { keywords: [a, b, c, d]} ... }
other valid structures are
{ meta: { keywords: "a,b,c,d"} ... }
and
{ meta: {keywords: "a"} ...}
I have this classes
public class Data {
#PropertyName("meta")
MetaData meta;
...
}
public class MetaData {
List<String> keywords;
...
}
and a custom deserializer
public static class CustomDeserilizer implements JsonDeserializer<MetaData>{
#Override
public MetaData deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
List<String> keywords = null;
Gson gson = new Gson();
MetaData metaData = gson.fromJson(json, AppMetaData.class);
JsonObject jsonObject = json.getAsJsonObject();
if (jsonObject.has("keywords")) {
JsonElement elem = jsonObject.get("keywords");
if (elem != null && !elem.isJsonNull()) {
if (jsonObject.get("keywords").isJsonArray()) {
keywords = gson.fromJson(jsonObject.get("keywords"), new TypeToken<List<String>>() {
}.getType());
} else {
String keywordString = gson.fromJson(jsonObject.get("keywords"), String.class);
keywords = new ArrayList<String>();
list.addAll(Arrays.asList(keywordString.split(",")));
}
}
}
metaData.setKeywords(keywords);
}
Then I try to apply the deserilizer:
Gson gson = new GsonBuilder()
.registerTypeAdapter(Data.class,new CustomDeserilizer())
.create();
But I get a parsing error , because is trying to deserialize Data instead of MetaData, how can I apply this deserializer to make it work right?
I solved it creating a deserializer for my class Data.
public static class DataDeserilizer implements JsonDeserializer {
#Override
public Data deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
Gson gson = new Gson();
Data data = gson.fromJson(json, Data.class);
JsonObject jsonObject = json.getAsJsonObject();
if (jsonObject.has("meta")) {
JsonElement elem = jsonObject.get("meta");
if (elem != null && !elem.isJsonNull()) {
Gson gsonDeserializer = new GsonBuilder()
.registerTypeAdapter(MetaData.class, new CustomDeserilizer())
.create();
gsonDeserializer.fromJson(jsonObject.get("meta"), Data.class);
}
}
return data;
}
}
And
Gson gson = new GsonBuilder()
.registerTypeAdapter(Data.class,new DataDeserilizer())
.create();
Pretty obvious, but is there a more elegant solution?
Firstly, rename your class to meta instead of metadata and make keywords String instead of List.Then use the following to map your JSonString into your object.
Gson gson = new GsonBuilder().create();
Meta meta = gson.from(yourJsonString,Meta.class);
In order to get keywords only, you need this.
JsonObject jsonObject = new JsonObject(yourJSonString);
String data = jsonObject.getJsonObject("meta").getString("keywords");
keywords is a JsonObject not an JsonArray so you can't directly map it
onto List. You can split the string to get keywords in an array.
String keywords[] = data.split(",");
Here's a concise solution that leverages Java inheritance to represent the nested structure; and therefore does not need to provide any actual instance member fields (mappings, etc) for capturing the nested String data that GSON maps.
Step 1: For readability, create an empty object to represent the nested mapping
public class StateRegionCitiesMap extends HashMap<String, List<String>> {
}
Step 2: Add the one line of actual code to do the mapping; no other serialize/deserialize logic to manage
protected void loadContent(JsonObject stateRegionsJsonObject) {
HashMap<String, StateRegionCitiesMap> stateRegionCitiesMap =
mGson.fromJson(
stateRegionsJsonObject,
new TypeToken<HashMap<String, StateRegionCitiesMap>>() {
}.getType()
);
}
Alternatively, you can skip the wrapper class altogether and just directly put <String, List<String>> in the GSON call. However, I find an explicit object helps to inform/remind whoever is reading the code, what the purpose is.
Example JSON:
The class StateRegionCitiesMap represents a multi-tier map structure for say:
[US State] -> [State-Region Key] -> [Sub-Region Key] -> CitiesArray[]
"CA": {
"Central CA": {
"Central Valley": [
"FRESNO",
"VISALIA"
],
"Sacramento Area": [
"SACRAMENTO",
"EL DORADO HILLS"
]
},
This suppose to achieve what you want easily. You should define an inner static class. You can keep nesting classes to define keywords as class Keywords, etc. Just remember to have a field in the containing class, i.e.
in your inner class have private Keywords keywords;
In your Main class:
Gson gson = new Gson();
Data data = gson.fromJson(SOME_JSON_STRING, Data.class);
In a class called Data:
public class Data {
private Meta meta;
static class Meta{
private String[] keywords;
}
}
I'm trying to parse some JSON data using gson in Java that has the following structure but by looking at examples online, I cannot find anything that does the job.
Would anyone be able to assist?
{
"data":{
"id":[
{
"stuff":{
},
"values":[
[
123,
456
],
[
123,
456
],
[
123,
456
],
],
"otherStuff":"blah"
}
]
}
}
You just need to create a Java class structure that represents the data in your JSON. In order to do that, I suggest you to copy your JSON into this online JSON Viewer and you'll see the structure of your JSON much clearer...
Basically you need these classes (pseudo-code):
class Response
Data data
class Data
List<ID> id
class ID
Stuff stuff
List<List<Integer>> values
String otherStuff
Note that attribute names in your classes must match the names of your JSON fields! You may add more attributes and classes according to your actual JSON structure... Also note that you need getters and setters for all your attributes!
Finally, you just need to parse the JSON into your Java class structure with:
Gson gson = new Gson();
Response response = gson.fromJson(yourJsonString, Response.class);
And that's it! Now you can access all your data within the response object using the getters and setters...
For example, in order to access the first value 456, you'll need to do:
int value = response.getData().getId().get(0).getValues().get(0).get(1);
Depending on what you are trying to do. You could just setup a POJO heirarchy that matches your json as seen here (Preferred method). Or, you could provide a custom deserializer. I only dealt with the id data as I assumed it was the tricky implementation in question. Just step through the json using the gson types, and build up the data you are trying to represent. The Data and Id classes are just pojos composed of and reflecting the properties in the original json string.
public class MyDeserializer implements JsonDeserializer<Data>
{
#Override
public Data deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException
{
final Gson gson = new Gson();
final JsonObject obj = je.getAsJsonObject(); //our original full json string
final JsonElement dataElement = obj.get("data");
final JsonElement idElement = dataElement.getAsJsonObject().get("id");
final JsonArray idArray = idElement.getAsJsonArray();
final List<Id> parsedData = new ArrayList<>();
for (Object object : idArray)
{
final JsonObject jsonObject = (JsonObject) object;
//can pass this into constructor of Id or through a setter
final JsonObject stuff = jsonObject.get("stuff").getAsJsonObject();
final JsonArray valuesArray = jsonObject.getAsJsonArray("values");
final Id id = new Id();
for (Object value : valuesArray)
{
final JsonArray nestedArray = (JsonArray)value;
final Integer[] nest = gson.fromJson(nestedArray, Integer[].class);
id.addNestedValues(nest);
}
parsedData.add(id);
}
return new Data(parsedData);
}
}
Test:
#Test
public void testMethod1()
{
final String values = "[[123, 456], [987, 654]]";
final String id = "[ {stuff: { }, values: " + values + ", otherstuff: 'stuff2' }]";
final String jsonString = "{data: {id:" + id + "}}";
System.out.println(jsonString);
final Gson gson = new GsonBuilder().registerTypeAdapter(Data.class, new MyDeserializer()).create();
System.out.println(gson.fromJson(jsonString, Data.class));
}
Result:
Data{ids=[Id {nestedList=[[123, 456], [987, 654]]}]}
POJO:
public class Data
{
private List<Id> ids;
public Data(List<Id> ids)
{
this.ids = ids;
}
#Override
public String toString()
{
return "Data{" + "ids=" + ids + '}';
}
}
public class Id
{
private List<Integer[]> nestedList;
public Id()
{
nestedList = new ArrayList<>();
}
public void addNestedValues(final Integer[] values)
{
nestedList.add(values);
}
#Override
public String toString()
{
final List<String> formattedOutput = new ArrayList();
for (Integer[] integers : nestedList)
{
formattedOutput.add(Arrays.asList(integers).toString());
}
return "Id {" + "nestedList=" + formattedOutput + '}';
}
}
I have jsons like:
{
"id": "AnId",
"type": "Button",
"parameter": {
"text" : "TestText"
}
}
parameters can be of any structure and they can contain further json objects or arrays, too, e.g:
{
"id": "AnId",
"type": "RadioGroup",
"parameter": {
"groupname": "AGroupName",
"selected": "r2",
"radioboxes": [
{
"id": "r1",
"label": "radio1"
},
{
"id": "r2",
"label": "radio2"
}
]
}
}
However, depending on the type, it is known what the parameters look like, e.g.
public class RadioGroup {
private String groupName;
// ..other primitive attributes here..
private Map<String, RadioBox> radioBoxes;
// ... getters/setters
}
public class RadioBox {
private String id;
private String label;
// ... getter/setters
}
BUT I don't know the type before I deserialized it!
I wonder what is the best way to deserialize input where I dont know whether I get button or radiogroup json?
Until now I just deserialize the given input (button or checkbox, not known yet) to Map<String, Object>, if it is in fact button it works fine, but if I provide the radiogroup as input, I will get gson's internal type ArrayList<StringMap> for the radioboxes. The primitive parameters like groupName are fine (String.class). But what can I do to get List<Map<String,Radiobox>> instead of ArrayList<StringMap> for the radioboxes parameter?
You need to set the Type of the custom class you're using. This is for serialising (just an example):
Gson gson = new GsonBuilder().create()
Type aType = new TypeToken</ClassName/>() {}.getType();
String json = gson.toJson(this, aType);
gson will then convert the variable types correctly. If you're using a custom class as a variable, you'll need to write a custom type adapter and register the type adapter with the GsonBuilder() instance.
To deserialise, you'll need the same process.
EDIT:
Class<?> klass = null;
try {
klass = Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new JsonParseException(e.getMessage());
}
you can parse arbitrary json using GSON JsonDeserializer. I've write model class for RadioGroup as following but you can change it according to your need.
public class RadioGroup {
private String id;
private String type;
// You can put groupName and selected properties into other class,
private String groupname;
private String selected;
// for button param parsing into Map
private Map<String,String> btnParam;
// for radion Btn just parsing into List
private List<RadioBox> radioboxes;
}
public class CustomDeserializer implements JsonDeserializer<RadioGroup> {
#Override
public RadioGroup deserialize(JsonElement json, Type arg1,
JsonDeserializationContext arg2) throws JsonParseException {
final JsonObject jsonObject = json.getAsJsonObject();
final String id= jsonObject.get("id").getAsString();
final String type= jsonObject.get("type").getAsString();
final RadioGroup radioGroup = new RadioGroup();
radioGroup.setId(id);
radioGroup.setType(type);
JsonElement jsonElementForParam = jsonObject.get("parameter");
JsonElement jsonElementForText = jsonElementForParam.getAsJsonObject().get("text");
if(null==jsonElementForText){
String groupname = jsonElementForParam.getAsJsonObject().get("groupname").getAsString();
String selected = jsonElementForParam.getAsJsonObject().get("selected").getAsString();
JsonElement jsonElementForRadioboxes = jsonElementForParam.getAsJsonObject().get("radioboxes");
radioGroup.setSelected(selected);
radioGroup.setGroupname(groupname);
Gson gson = new Gson();
Type typeOf = new TypeToken<List<RadioBox>>(){}.getType();
List<RadioBox> radioboxes =gson.fromJson(jsonElementForRadioboxes, typeOf);
radioGroup.setRadioboxes(radioboxes);
} else if (null!=jsonElementForText) {
Gson gson = new Gson();
Type typeOf = new TypeToken<Map<String,String>>(){}.getType();
Map<String,String> map = gson.fromJson(jsonElementForParam, typeOf);
radioGroup.setBtnParam(map);
}
return radioGroup;
}
}
Finally Parse it
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(RadioGroup.class, new CustomDeserializer());
Gson gson = gsonBuilder.create();
RadioGroup radioGroup = gson.fromJson(buttonJson, RadioGroup.class);
I have this structure of my JSON response string:
{
"1":{
"data1":"1","data2":"test1", ...
},
"2":{
"data1":"6","data2":"test2", ...
},
...
}
And I want to get the values to put into an ArrayList<MyItem>. I use GSON and normally I can do it in this way:
ArrayList<MyItem> items =
gson.fromJson(jsonString, new TypeToken<ArrayList<MyItem>>() {}.getType());
The problem is, that it does not work, because my JSON String has numbers as keys, but I only want to get the values to put into the ArrayList (unfortunately, the JSON string can not be changed by myself). How can I do this efficiently?
I'd probably deserialize the JSON into a java.util.Map, get the values from the Map as a Collection using the Map.values() method, and then create a new ArrayList using the constructor that takes a Collection.
Write a custom deserializer.
class MyItem
{
String data1;
String data2;
// ...
}
class MyJSONList extends ArrayList<MyItem> {}
class MyDeserializer implements JsonDeserializer<MyJSONList>
{
public MyJSONList deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
throws JsonParseException
{
MyJSONList list = new MyJSONList();
for (Entry<String, JsonElement> e : je.getAsJsonObject().entrySet())
{
list.add((MyItem)jdc.deserialize(e.getValue(), MyItem.class));
}
return list;
}
}
Example:
String json = "{\"1\":{\"data1\":\"1\",\"data2\":\"test1\"},\"2\":{\"data1\":\"6\",\"data2\":\"test2\"}}";
Gson g = new GsonBuilder()
.registerTypeAdapter(MyJSONList.class, new MyDeserializer())
.create();
MyJSONList l = g.fromJson(json, MyJSONList.class);
for (MyItem i : l)
{
System.out.println(i.data2);
}
Output:
test1test2