I trying to deserialize this json to array of objects:
[{
"name": "item 1",
"tags": ["tag1"]
},
{
"name": "item 2",
"tags": ["tag1","tag2"]
},
{
"name": "item 3",
"tags": []
},
{
"name": "item 4",
"tags": ""
}]
My java class looks like this:
public class MyObject
{
#Expose
private String name;
#Expose
private List<String> tags = new ArrayList<String>();
}
The problem is json's tags property which can be just empty string or array. Right now gson gives me error: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING
How should I deserialize this json?
I do not have any control to this json, it comes from 3rd pary api.
I do not have any control to this json, it comes from 3rd pary api.
If you don't have the control over the data, your best solution is to create a custom deserializer in my opinion:
class MyObjectDeserializer implements JsonDeserializer<MyObject> {
#Override
public MyObject deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject jObj = json.getAsJsonObject();
JsonElement jElement = jObj.get("tags");
List<String> tags = Collections.emptyList();
if(jElement.isJsonArray()) {
tags = context.deserialize(jElement.getAsJsonArray(), new TypeToken<List<String>>(){}.getType());
}
//assuming there is an appropriate constructor
return new MyObject(jObj.getAsJsonPrimitive("name").getAsString(), tags);
}
}
What it does it that it checks whether "tags" is a JsonArray or not. If it's the case, it deserializes it as usual, otherwise you don't touch it and just create your object with an empty list.
Once you've written that, you need to register it within the JSON parser:
Gson gson = new GsonBuilder().registerTypeAdapter(MyObject.class, new MyObjectDeserializer()).create();
//here json is a String that contains your input
List<MyObject> myObjects = gson.fromJson(json, new TypeToken<List<MyObject>>(){}.getType());
Running it, I get as output:
MyObject{name='item 1', tags=[tag1]}
MyObject{name='item 2', tags=[tag1, tag2]}
MyObject{name='item 3', tags=[]}
MyObject{name='item 4', tags=[]}
Before converting the json into object replace the string "tags": "" with "tags": []
Use GSON's fromJson() method to de serialize your JSON.
You can better understand this by the example given below:
import java.util.ArrayList;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
public class JsonToJava {
/**
* #param args
*/
public static void main(String[] args) {
String json = "[{\"firstName\":\"John\", \"lastName\":\"Doe\", \"id\":[\"10\",\"20\",\"30\"]},"
+ "{\"firstName\":\"Anna\", \"lastName\":\"Smith\", \"id\":[\"40\",\"50\",\"60\"]},"
+ "{\"firstName\":\"Peter\", \"lastName\":\"Jones\", \"id\":[\"70\",\"80\",\"90\"]},"
+ "{\"firstName\":\"Ankur\", \"lastName\":\"Mahajan\", \"id\":[\"100\",\"200\",\"300\"]},"
+ "{\"firstName\":\"Abhishek\", \"lastName\":\"Mahajan\", \"id\":[\"400\",\"500\",\"600\"]}]";
jsonToJava(json);
}
private static void jsonToJava(String json) {
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonArray jArray = parser.parse(json).getAsJsonArray();
ArrayList<POJO> lcs = new ArrayList<POJO>();
for (JsonElement obj : jArray) {
POJO cse = gson.fromJson(obj, POJO.class);
lcs.add(cse);
}
for (POJO pojo : lcs) {
System.out.println(pojo.getFirstName() + ", " + pojo.getLastName()
+ ", " + pojo.getId());
}
}
}
POJO class:
public class POJO {
private String firstName;
private String lastName;
private String[] id;
//Getters and Setters.
I hope this will solve your issue.
You are mixing datatypes. You cant have both an Array and a string. Change
"tags": ""
to
"tags": null
and you are good to go.
Use Jacskon Object Mapper
See below simple example
[http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/][1]
Jackson type safety is way better than Gson. At times you will stackoverflow in Gson.
Related
I have a JSON object stored in db in the form of string. I am using it to create dynamic form in the UI. Now the problem is I want to change some values in it based on other changes happening on the application. So suppose I updated label for the field, then I have to get this JSON and change that here.
This would be easy If I have stored same type of objects in this json, but my JSON is like follows:
[{
"name": "someName",
"xtype": "keyvaluecombo",
"fieldLabel": "Some Title",
"refType": "YES_NO",
"multiSelect": false,
"helpText": ""
},
{
"name": "someName2",
"xtype": "keyvaluecombo",
"fieldLabel": "Some Title2",
"refType": "YES_NO",
"multiSelect": false,
"helpText": ""
},
{
"xtype": "datefield",
"fieldLabel": "Joining Date",
"name": "joiningDate",
"submitFormat": "Y-m-d"
},
{
"xtype": "userselectioncombo",
"fieldLabel": "Selection",
"name": "selections",
"filterBy": {
"functions": [
"select"
]
}
}]
Now this is stored as String in db, what is efficient way of changing fieldLabel based on name. I could have tried working on it as string only and use regular expression, but that didn't feel right.
You should write a bean class, which should be mapping to you Json object like,
public class abc {
private String name;
private String xtype;
private String fieldLabel;
........
}
Then you should use
import java.lang.reflect.Type;
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
Type type = new TypeToken<List<abc>>() {
}.getType();
List<abc> abcList = gson.fromJson(confTemplate,
type); // confTemplate is your Json object you get from DB
this will get the list of beans.
for (abc abcData : abcList ) {
// you can do your stuff
}
First of all the best way, changing your database design with a new one that suits your model. Not keeping json in your database as a column. But if you can't do that, because sometimes it's impossible to change old db designs you can trace the following way.
And of course you should read your json from db before start and save it again after the below process.
Create a custom object that suits your model.
public class MyObject{
private String name;
private String fieldLabel;
public String getFieldLabel(){
return fieldLabel;
}
public void setFieldLabel( String fieldLabel ){
this.fieldLabel = fieldLabel;
}
public String getName(){
return name;
}
public void setName( String name ){
this.name = name;
}
// bla bla other fields
Convert your json into your object, and vice versa see the code example below:
public static void main( String[] args ){
Gson gson = new Gson();
String yourJson = "[{'name':'someName','xtype':'keyvaluecombo','fieldLabel':'Some Title','refType':'YES_NO','multiSelect':false,'helpText':''},{'name':'someName2','xtype':'keyvaluecombo','fieldLabel':'Some Title2','refType':'YES_NO','multiSelect':false,'helpText':''},{'xtype':'datefield','fieldLabel':'Joining Date','name':'joiningDate','submitFormat':'Y-m-d'},{'xtype':'userselectioncombo','fieldLabel':'Selection','name':'selections','filterBy':{'functions':['select']}}]";
// changing single quotes with double ones.
yourJson = yourJson.replaceAll( "'", "\"" );
JsonArray jsonArray = new JsonParser().parse( yourJson ).getAsJsonArray();
List<MyObject> result = new ArrayList<MyObject>();
for( JsonElement jsonElement : jsonArray ){
MyObject myObject = gson.fromJson( jsonElement, MyObject.class );
// change fields as you wish
if( myObject.getName().equals( "someName" ) ){
myObject.setFieldLabel( "TEST" );
}
// add it to another list
result.add( myObject );
}
// convert into another json again..
System.out.println( gson.toJson( result ) );
}
Easy to realize that Object in your list have attributes below:
"name"
"xtype"
"fieldLabel"
"refType"
"multiSelect"
"helpText"
"submitFormat"
"filterBy"
So you can create an Object which has over attributes. Using ObjectMapper for deserialize the list:
ObjectMapper mapper = new ObjectMapper();
mapper.readValue(json, new TypeReference<ArrayList<T>>() {})
After have a list object you can loop for change any attribute or change attribute of an specific item you want.
I am in trouble. I can not deserialize this object that I return json from an http request. Can anyone help me?
I downloaded and added to the libs folder gson_2.2.4.jar.
We insert the object json
{
"returnCode": 0,
"data": [
{
"token": "aaaaa =",
"code": "xx",
"id": ""
}
],
"errorMsg": ""
}
You need to create a class of data object, for example
public class DataObj {
public String token;
public String code;
public String id;
}
and then create another class for the whole json, for example
public class MyObj {
public int returnCode;
public DataObj[] data;
public String errorMsg;
}
then create an object of MyObj and use deserializer from GSON to read json,
for example:
GSON gson = new GSON();
MyObj newMyObj = gson.fromJson(jsonString, MyObj.class);
Where jsonString contains the json object as string.
(#Shivam Verma thanks for your edit)
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 the followed snipets of Json String:
{
"networks": {
"tech11": {
"id": "1",
"name": "IDEN"
},
"tech12": {
"id": "2",
"name": "EVDO_B"
}
}
}
I use some methods to convert this String to Object:
private static Gson mGson = new Gson();
...
public static WebObjectResponse convertJsonToObject(String jsonString) {
WebObjectResponse webObjectResponse = null;
if(jsonString != null && jsonString.length() > 1){
webObjectResponse = mGson.fromJson(jsonString, WebObjectResponse.class);
}
return webObjectResponse;
}
Where WebObjectResponse is class that should represent above mentioned String.
Its not complicated if I get static fields.
But in my case the values have different names: tech11, tech12 ....
I can use #SerializedName but its works in specific cases like convert "class" to "class_".
As you see networks Object defined as list of tech Objects but with different post-fix.
public class WebObjectResponse{
private DataInfoList networks = null;
}
This is static implementation, i defined 2 values tech11 and tech12 but next response might be techXX
public class DataInfoList {
private DataInfo tech11 = null;
private DataInfo tech12 = null;
}
public class DataInfo {
private String id = null;
private String name = null;
}
What is the good way to convert current Json String to Object where list of elements are Objects too and have different names?
Thank you.
Use a Map!
I would do the following
public class WebObjectResponse {
private Map<String, DataInfo> networks;
}
public class DataInfo {
private String id = null;
private String name = null;
}
// later
Gson gson = new Gson();
String json = "{\"networks\": {\"tech11\": { \"id\": \"1\",\"name\": \"IDEN\" }, \"tech12\": { \"id\": \"2\", \"name\": \"EVDO_B\" } }}";
WebObjectResponse response = gson.fromJson(json, WebObjectResponse .class);
For each object in json networks, a new entry will be added to the Map field of your class WebObjectResponse. You then reference them by techXX or iterate through the keyset.
Assuming a structure like this
{
"networks": {
"tech11": {
"id": "1",
"name": "IDEN"
},
"tech12": {
"id": "2",
"name": "EVDO_B"
},
"tech13": {
"id": "3",
"name": "WOHOO"
}, ...
}
}
We would need your class structure for more details.
As far as I am aware, I think you will need to have some mappings defined somewhere (I used xml's) and then try to match json with one of the mappings to create objects.
Google gson is good. I did it in Jackson
Also, converting objects should be trivial. But since you might have variable fields like tech11 and tech12 , you might want to store the "network" value as a string and then extract fields out of it when required.
Hope I could help.
Edit : Sotirious nails it.
Please use this link for converting SON Response to Java POJO class
http://www.jsonschema2pojo.org/
I have a JSON response that looks like this. I want to extract the values of "text" and put them into a Set of Strings (i.e. I don't necessarily need the entire JSON to be derialised).
I am using the GSON library
So far my method looks like this (It's obviously wrong):
public static Response deserialise(String json){
Gson gson = new Gson();
Response r = gson.fromJson(json, Response.class);
return r;
}
I am calling deserialise with this:
Response r = deserialise(json);
System.out.println("[status]: "+r.getStatus()); // works fine
Collection<Keyword> coll = r.getKeywords();
Iterator<Keyword> itr = coll.iterator();
while(itr.hasNext()){
System.out.println(itr.next().getWord()); //prints null every time
}
Response is a class with the following member variables (with getters and setters):
private String status;
private String usage;
private String language;
private Collection<Keyword> keywords;
Keyword is a class with the following member variables (with getters and setters):
private String word;
private String relevance;
The JSON looks like this:
{
"status": "OK",
"usage": "By accessing AlchemyAPI or using information generated by AlchemyAPI, you are agreeing to be bound by the AlchemyAPI Terms of Use: http://www.alchemyapi.com/company/terms.html",
"url": "http://www.theage.com.au/world/aussie-trying-to-make-a-difference-gunned-down-20110510-1egnv.html",
"language": "english",
"keywords": [
{
"text": "Mr McNichols",
"relevance": "0.99441"
},
{
"text": "Ms Benton",
"relevance": "0.392337"
},
{
"text": "Detroit",
"relevance": "0.363931"
},
{
"text": "Crocodile Hunter",
"relevance": "0.350197"
}
]
}
The problem is that the Collection of Keywords returns null values - although it seems to have the correct size, which is positive.
This just works:
public class Keyword {
public String text;
public String relevance;
}
public class MyJSON {
public String status;
public String usage;
public String language;
public Collection<Keyword> keywords;
}
In the main method
String str = "{\"status\": \"OK\","+
"\"usage\": \"By accessing AlchemyAPI or using information generated by AlchemyAPI, you are agreeing to be bound by the AlchemyAPI Terms of Use: http://www.alchemyapi.com/company/terms.html\","+
"\"url\": \"http://www.theage.com.au/world/aussie-trying-to-make-a-difference-gunned-down-20110510-1egnv.html\","+
"\"language\": \"english\","+
"\"keywords\": ["+
"{"+
"\"text\": \"Mr McNichols\","+
"\"relevance\": \"0.99441\""+
"},"+
"{"+
"\"text\": \"Ms Benton\","+
"\"relevance\": \"0.392337\""+
"},"+
"{"+
"\"text\": \"Detroit\","+
"\"relevance\": \"0.363931\""+
"},"+
"{"+
"\"text\": \"Crocodile Hunter\","+
"\"relevance\": \"0.350197\""+
"}"+
"]"+
"}";
Gson gson = new Gson();
MyJSON mj = gson.fromJson(str, MyJSON.class);
System.out.println(mj.language);
for(Keyword k: mj.keywords)
System.out.println(k.text+":"+k.relevance);
This prints
english
Mr McNichols:0.99441
Ms Benton:0.392337
Detroit:0.363931
Crocodile Hunter:0.350197
Look carefully at my Keyword class!(and my JSON string starts with {).
This was all down to my own stupidity.
In the keyword class I called the variable word when it should have been text in order to map properly to the JSON.