I have an JSON array of arrays where none of the elements are named (it's just a pure array)
[
["http://test.com/","rj76-22dk"],
["http://othertest.com/","v287-28n3"]
]
In Java, I'd like to parse this JSON into an array of connectionobjs, where the connectionobj class looks like this:
public static class connectionOptions {
String URL, RESID;
}
I looked through the GSON documentation, but couldn't seem to find anything pertinent to parsing a JSON array into anything other than another Java Array. I want to parse the JSON array into a Java Object, not an array.
Is there a way to do this using Google's GSON?
I don't recommend this at all. You should try to have appropriate JSON that maps correctly to Pojos.
If you can't change your JSON format, you'll need to register a custom TypeAdapter that can do the conversion. Something like
class ConnectionOptionsTypeAdapter extends TypeAdapter<ConnectionOptions> {
#Override
public void write(JsonWriter out, ConnectionOptions value)
throws IOException {
// implement if you need it
}
#Override
public ConnectionOptions read(JsonReader in) throws IOException {
final ConnectionOptions connectionOptions = new ConnectionOptions();
in.beginArray();
connectionOptions.URL = in.nextString();
connectionOptions.RESID = in.nextString();
in.endArray();
return connectionOptions;
}
}
Then just register it
GsonBuilder gsonBuilder = new GsonBuilder().registerTypeAdapter(
ConnectionOptions.class, new ConnectionOptionsTypeAdapter());
Gson gson = gsonBuilder.create();
and use it.
Deserialize your JSON as an ConnectionOptions[] or List<ConnectionOptions>.
I've change your class name to ConnectionOptions to follow Java naming conventions.
You should give a customized Deserializer.
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.Collection;
public class TestGson {
public static class ConnectionOptions {
String URL, RESID;
#Override
public String toString() {
return "ConnectionOptions{URL='" + URL + "', RESID='" + RESID + "'}";
}
}
private static class ConnOptsDeserializer implements JsonDeserializer<ConnectionOptions> {
#Override
public ConnectionOptions deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
ConnectionOptions connOpts = new TestGson.ConnectionOptions();
JsonArray array = json.getAsJsonArray();
connOpts.URL = array.get(0).getAsString();
connOpts.RESID = array.get(1).getAsString();
return connOpts;
}
}
public static void main(String[] args) {
String json = "[[\"http://test.com/\",\"rj76-22dk\"],\n" +
" [\"http://othertest.com/\",\"v287-28n3\"]]";
GsonBuilder gsonb = new GsonBuilder();
gsonb.registerTypeAdapter(ConnectionOptions.class, new ConnOptsDeserializer());
Gson gson = gsonb.create();
Type collectionType = new TypeToken<Collection<ConnectionOptions>>(){}.getType();
Collection<ConnectionOptions> connList = gson.fromJson(json, collectionType);
System.out.println("connList = " + connList);
}
}
Related
This question already has answers here:
How to prevent Gson from expressing integers as floats
(10 answers)
Closed 4 years ago.
I have some JSON string snippets which could look like this:
{label: "My Label"}
{maxlength: 5}
{contact: {name: "John", "age": 5, children: [{"name": "Mary"]}}
etc, i.e. it could be any JSON object with any key names or value types.
Right now I am deserializing doing something pretty simple like this:
final Gson gson = new Gson();
Object newValue = gson.fromJson(stringValue, Object.class);
And this is working for 99% of the use cases. But as is mentioned here, it is converting any integers to doubles.
I'm fine registering a type adapter as is recommended elsewhere. So I wrote the following:
final Gson gson = new GsonBuilder()
.registerTypeAdapter(Object.class, new DoubleToInt())
.create();
Object newValue = gson.fromJson(stringValue, Object.class);
private static class DoubleToInt implements JsonDeserializer<Object>{
#Override
public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
// do my custom stuff here
return json;
}
}
But this isn't working at all. It's like the type adapter is not even getting registered because breakpoints never even hit in the deserialize method.
As the post you link suggested, you should create custom class, so I did and it's working correctly:
public class Test {
public static void main(String[] args) {
final Gson gson = new GsonBuilder()
.registerTypeAdapter(MyClass.class, new DoubleToInt())
.create();
String stringValue = "{contact: {name: \"John\", \"age\": 5, children: [{\"name\": \"Mary\"}]}}";
MyClass newValue = gson.fromJson(stringValue, MyClass.class);
System.out.println(newValue.toString());
}
private static class DoubleToInt implements JsonDeserializer<MyClass> {
#Override
public MyClass deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
// do my custom stuff here
return new MyClass(json);
}
}
}
class MyClass {
private JsonElement element;
MyClass(JsonElement element) {
this.element = element;
}
#Override
public String toString() {
return element.toString();
}
}
In the post you linked, they suggested using a custom class in order to tell what data types you should use instead of using Object.class. Have you tried doing that?
class CustomClass{
String label;
int maxLength;
...
}
Object newValue = gson.fromJson(stringValue, CustomClass.class);
When reading a JSON :
{"field":"value"}
into a String field :
public class Test {
private String field;
}
using Gson.fromJson it works as intended and the member String field gets the value "value".
My question is, is there a way to read the same JSON into a custom class so that the custom class object can be constructed with the String value? e.g.
public class Test {
private MyField<String> field;
}
public class MyField<T> {
private T value;
public MyField(T v) {
value = v;
}
}
The reason being the String class is final and cannot be extended, yet I don't want the JSON to be changed into this :
{"field":{"value":"value"}}
If there is a way to extend the String class, it is the best. Otherwise, will need a way for Gson to read string into a custom class that can be constructed by string. Something to do with writing a custom TypeAdapter?
You can use custom JsonDeserializer, JsonSerializer. Here is simple demo version:
static class MyFieldAsValueTypeAdapter<T> implements
JsonDeserializer<MyField<T>>, JsonSerializer<MyField<T>> {
private Gson gson = new Gson();
#Override
public MyField<T> deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context)
throws JsonParseException {
JsonObject obj = new JsonObject();
obj.add("value", json);
return gson.fromJson(obj, typeOfT);
}
#Override
public JsonElement serialize(MyField<T> src, Type typeOfSrc,
JsonSerializationContext context) {
return context.serialize(src.getValue());
}
}
public static void main(String[] args) {
GsonBuilder b = new GsonBuilder();
b.registerTypeAdapter(MyField.class , new MyFieldAsValueTypeAdapter());
Gson gson = b.create();
String json = "{\"field\":\"value1\"}";
Test test = gson.fromJson(json, Test.class);
}
Be careful with internal Gson gson = new Gson(). If you have some other setup, you will need to register it on internal version or pass default MyField deserializer/serializer to your custom implementation.
I recently started using GSON and i am facing an issue in deserialising this json. The issue is at below json element which contains spaces in field names
{
longDescriptionNonHTML: {
What it is:: " An oil-free, color-tinted moisturizer."
What it does:: " This lightweight foundation can be reapplied as necessary and offers moisturizing, oil-control coverage for sensitive or acne prone skin."
}
{
SKUType: "restricted"
brandName: "Laura Mercier"
brandId: 5809
skuID: "1228139"
availableInStore: 0
topSellerRank: 8297
productName: "Tinted Moisturizer - Oil Free"
shade_description: "Porcelain"
shortDescription: "What it is: An oil-free, color-tinted moisturizer.What it does: This lightweight foundation can be reapplied as necessary and offers moisturizing, oil-control coverage for sensitive or acne prone skin."
size: "1.7 oz"
listPrice: 0
salePrice: 0
image: "http://www.sephora.com/productimages/sku/s1228139-main-hero.jpg"
rating_product: 4.3
online_store: "http://www.sephora.com/tinted-moisturizer-oil-free-P310929?skuId=1228139&lang=en"
imageBrand: "http://www.sephora.com/contentimages/brands/lauramercier/5809_logo_279.png"
productId: "P310929"
formulation: "Liquid"
spf: ""
coverage: "Sheer, Medium"
finish: "Matte, Natural"
ingredients: "Vitamin C"
skintype: "Combination, Normal, Oily"
longDescription: "<b>What it is:</b><br> An oil-free, color-tinted moisturizer.<br><br><b>What it does:</b><br> This lightweight foundation can be reapplied as necessary and offers moisturizing, oil-control coverage for sensitive or acne prone skin."
longDescriptionNonHTML: {
What it is:: " An oil-free, color-tinted moisturizer."
What it does:: " This lightweight foundation can be reapplied as necessary and offers moisturizing, oil-control coverage for sensitive or acne prone skin."
}-
skintone: "2Y03"
longIngredientsDesc: ""
language: "en"
isPrimarySkintone: 1
isDefaultSku: 0
storeonly: 0
}
I wrote below POJO
public class LongDescriptionNonHTML {
#SerializedName("What it is:")
private String whatItIs;
#SerializedName("What it does:")
private String whatItDoes;
public LongDescriptionNonHTML(String a,String b){
whatItDoes=a;
whatItDoes=b;
}
public String getWhatItIs() {
return whatItIs;
}
public void setWhatItIs(String whatItIs) {
this.whatItIs = whatItIs;
}
public String getWhatItDoes() {
return whatItDoes;
}
public void setWhatItDoes(String whatItDoes) {
this.whatItDoes = whatItDoes;
}
}
Also for deserialise it I wrote below code
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonReader reader = new JsonReader(new FileReader(fileName));
LongDescriptionNonHTML obj=gson.fromJson(reader, LongDescriptionNonHTML.class);
System.out.println(obj.getWhatItDoes());
But it is printing null value.
It may be useful for you to convert JSON to POJO Class.
public static <T> T convertJSON2POJOClass(String json, Class<T> type)
{
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.serializeNulls();
try{
return (T) gson.fromJson(json, type);
} catch (Exception expJSONToClassConvertor) {
baseDAO.getInstance().logAnError("common", baseDAO.getInstance().stackTraceToString(expJSONToClassConvertor));
return null;
}
If what you posted is the actual JSON and your whole actual POJO code then you have a mismatch between your JSON and POJO structure:
You are asking Gson to produce a LongDescriptionNonHTML from your JSON.
But your JSON is not just that - it actually is some other object that contains a LongDescriptionNonHTML as the value for the key longDescriptionNonHTML.
I solved it
public class LongDescriptionNonHTML {
//#SerializedName("What it is:")
private String whatItIs;
//#SerializedName("What it does:")
private String whatItDoes;
public LongDescriptionNonHTML(String a,String b){
whatItDoes=a;
whatItDoes=b;
}
public String getWhatItIs() {
return whatItIs;
}
public void setWhatItIs(String whatItIs) {
this.whatItIs = whatItIs;
}
public String getWhatItDoes() {
return whatItDoes;
}
public void setWhatItDoes(String whatItDoes) {
this.whatItDoes = whatItDoes;
}
Then created a deserializer
public class FooDeserializer implements JsonDeserializer{
#Override
public LongDescriptionNonHTML deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context)
throws JsonParseException {
JsonObject jo = (JsonObject)json;
String a =jo.get("What it is:").getAsString();
String b =jo.get("What it does:").getAsString();
return new LongDescriptionNonHTML(a,b);
}
}
and then finally tested it
public class jsonTest {
#Test
public void testJson() throws FileNotFoundException{
String fileName="/Users/User/Documents/workspace/simulator/book.json";
Gson gson = new GsonBuilder().setPrettyPrinting().registerTypeAdapter(LongDescriptionNonHTML.class, new FooDeserializer()).create();
JsonReader reader = new JsonReader(new FileReader(fileName));
LongDescriptionNonHTML obj=gson.fromJson(reader, LongDescriptionNonHTML.class);
System.out.println(obj.getWhatItDoes());
}
}
I have class with some properties, for example:
public class MyClass {
public int number;
public String s;
}
and I want to convert Map of this class to json. for example:
Map<String, MyClass> map = new HashMap();
map.put("sss", new MyClass(1, "blabla");
json j = new json(map);
and I want the output to be like:
{"sss":{"number":"1","s":"blabla"}}
someone know how to do that in JAVA? I tried with JSONObject and with Gson but did not work for me.
you can use toJson() method of Gson class to convert a java object to json ,see the example below ,
public class SomeObject {
private int data1 = 100;
private String data2 = "hello";
private List<String> list = new ArrayList<String>() {
{
add("String 1");
add("String 2");
add("String 3");
}
};
//getter and setter methods
#Override
public String toString() {
return "SomeObject [data1=" + data1 + ", data2=" + data2 + ", list="
+ list + "]";
}
}
i will convert the above class' object to json , getter and setter methods are useful when you are converting the json back to java object .
public static void main(String[] args) {
SomeObject obj = new SomeObject();
Gson gson = new Gson();
// convert java object to JSON format,
// and returned as JSON formatted string
String json = gson.toJson(obj);
System.out.println(json);
}
output :
{"data1":100,"data2":"hello","list":["String 1","String 2","String 3"]}
Using Gson:
Gson gson = new GsonBuilder().create();
String json = gson.toJson(map);
You have to fix, parenthesis issue.
map.put("sss", new MyClass(1,"test")); //observe 2 braces at the end!
Following code should do the trick for you,
Gson gson = new Gson();
String myJson = gson.toJson(map);
Output:
{"sss":{"number":1,"s":"test"}}
Implement some custom toJSON() method for each class as shown below:
public class MyClass1 {
String number;
String name;
public MyClass1(String number, String name){
this.number = number;
this.name = name;
}
public JSONObject toJSON() throws JSONException {
return new JSONObject("{\"number\" : \""+this.number+"\", \"name\":\""+this.name+"\"}");
}
}
And then just use it to convert your map to jsonObject:
public class MapToJSON {
public static void main(String[] args) throws JSONException {
Map<String, JSONObject> map = new HashMap<String, JSONObject>();
map.put("sss", new MyClass1("1", "Hello").toJSON());
System.out.println(new JSONObject(map));
}
}
I found the way how to do that:
import com.google.gson.Gson;
import org.json.JSONObject;
Gson gson = new Gson();
map.put("sss", new JSONObject(gson.toJson(new MyClass(1, "Hello"))));
map.put("aaa", new JSONObject(gson.toJson(new MyClass(2, "blabla"))));
String output = new JSONObject(map).toString();
and now the output is correct.
Thanks a lot to all the people that tried to help me with this problem...
I want to parse JSON arrays and using gson. Firstly, I can log JSON output, server is responsing to client clearly.
Here is my JSON output:
[
{
id : '1',
title: 'sample title',
....
},
{
id : '2',
title: 'sample title',
....
},
...
]
I tried this structure for parsing. A class, which depends on single array and ArrayList for all JSONArray.
public class PostEntity {
private ArrayList<Post> postList = new ArrayList<Post>();
public List<Post> getPostList() {
return postList;
}
public void setPostList(List<Post> postList) {
this.postList = (ArrayList<Post>)postList;
}
}
Post class:
public class Post {
private String id;
private String title;
/* getters & setters */
}
When I try to use gson no error, no warning and no log:
GsonBuilder gsonb = new GsonBuilder();
Gson gson = gsonb.create();
PostEntity postEnt;
JSONObject jsonObj = new JSONObject(jsonOutput);
postEnt = gson.fromJson(jsonObj.toString(), PostEntity.class);
Log.d("postLog", postEnt.getPostList().get(0).getId());
What's wrong, how can I solve?
You can parse the JSONArray directly, don't need to wrap your Post class with PostEntity one more time and don't need new JSONObject().toString() either:
Gson gson = new Gson();
String jsonOutput = "Your JSON String";
Type listType = new TypeToken<List<Post>>(){}.getType();
List<Post> posts = gson.fromJson(jsonOutput, listType);
I was looking for a way to parse object arrays in a more generic way; here is my contribution:
CollectionDeserializer.java:
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
public class CollectionDeserializer implements JsonDeserializer<Collection<?>> {
#Override
public Collection<?> deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
Type realType = ((ParameterizedType)typeOfT).getActualTypeArguments()[0];
return parseAsArrayList(json, realType);
}
/**
* #param serializedData
* #param type
* #return
*/
#SuppressWarnings("unchecked")
public <T> ArrayList<T> parseAsArrayList(JsonElement json, T type) {
ArrayList<T> newArray = new ArrayList<T>();
Gson gson = new Gson();
JsonArray array= json.getAsJsonArray();
Iterator<JsonElement> iterator = array.iterator();
while(iterator.hasNext()){
JsonElement json2 = (JsonElement)iterator.next();
T object = (T) gson.fromJson(json2, (Class<?>)type);
newArray.add(object);
}
return newArray;
}
}
JSONParsingTest.java:
public class JSONParsingTest {
List<World> worlds;
#Test
public void grantThatDeserializerWorksAndParseObjectArrays(){
String worldAsString = "{\"worlds\": [" +
"{\"name\":\"name1\",\"id\":1}," +
"{\"name\":\"name2\",\"id\":2}," +
"{\"name\":\"name3\",\"id\":3}" +
"]}";
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Collection.class, new CollectionDeserializer());
Gson gson = builder.create();
Object decoded = gson.fromJson((String)worldAsString, JSONParsingTest.class);
assertNotNull(decoded);
assertTrue(JSONParsingTest.class.isInstance(decoded));
JSONParsingTest decodedObject = (JSONParsingTest)decoded;
assertEquals(3, decodedObject.worlds.size());
assertEquals((Long)2L, decodedObject.worlds.get(1).getId());
}
}
World.java:
public class World {
private String name;
private Long id;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
}
To conver in Object Array
Gson gson=new Gson();
ElementType [] refVar=gson.fromJson(jsonString,ElementType[].class);
To convert as post type
Gson gson=new Gson();
Post [] refVar=gson.fromJson(jsonString,Post[].class);
To read it as List of objects TypeToken can be used
List<Post> posts=(List<Post>)gson.fromJson(jsonString,
new TypeToken<List<Post>>(){}.getType());
Type listType = new TypeToken<List<Post>>() {}.getType();
List<Post> posts = new Gson().fromJson(jsonOutput.toString(), listType);
Some of the answers of this post are valid, but using TypeToken, the Gson library generates a Tree objects whit unreal types for your application.
To get it I had to read the array and convert one by one the objects inside the array. Of course this method is not the fastest and I don't recommend to use it if you have the array is too big, but it worked for me.
It is necessary to include the Json library in the project. If you are developing on Android, it is included:
/**
* Convert JSON string to a list of objects
* #param sJson String sJson to be converted
* #param tClass Class
* #return List<T> list of objects generated or null if there was an error
*/
public static <T> List<T> convertFromJsonArray(String sJson, Class<T> tClass){
try{
Gson gson = new Gson();
List<T> listObjects = new ArrayList<>();
//read each object of array with Json library
JSONArray jsonArray = new JSONArray(sJson);
for(int i=0; i<jsonArray.length(); i++){
//get the object
JSONObject jsonObject = jsonArray.getJSONObject(i);
//get string of object from Json library to convert it to real object with Gson library
listObjects.add(gson.fromJson(jsonObject.toString(), tClass));
}
//return list with all generated objects
return listObjects;
}catch(Exception e){
e.printStackTrace();
}
//error: return null
return null;
}
You can easily do this in Kotlin using the following code:
val fileData = "your_json_string"
val gson = GsonBuilder().create()
val packagesArray = gson.fromJson(fileData , Array<YourClass>::class.java).toList()
Basically, you only need to provide an Array of YourClass objects.
[
{
id : '1',
title: 'sample title',
....
},
{
id : '2',
title: 'sample title',
....
},
...
]
Check Easy code for this output
Gson gson=new GsonBuilder().create();
List<Post> list= Arrays.asList(gson.fromJson(yourResponse.toString,Post[].class));
in Kotlin :
val jsonArrayString = "['A','B','C']"
val gson = Gson()
val listType: Type = object : TypeToken<List<String?>?>() {}.getType()
val stringList : List<String> = gson.fromJson(
jsonArrayString,
listType)
you can get List value without using Type object.
EvalClassName[] evalClassName;
ArrayList<EvalClassName> list;
evalClassName= new Gson().fromJson(JSONArrayValue.toString(),EvalClassName[].class);
list = new ArrayList<>(Arrays.asList(evalClassName));
I have tested it and it is working.