just_do_IT
just_do_IT

Reputation: 91

How do I convert a JSON String into a GSON Object?

I'm going to develop a webservice and its input should be described in JSON which i want to use in my inner logic as a GSON Object. So whoever uses the webservice will send a request with some infmorations like this:

{
"glossary": {
    "title": "example glossary",
    "GlossDiv": {
        "title": "S",
        "GlossList": {
            "GlossEntry": {
                "ID": "SGML",
                "SortAs": "SGML",
                "GlossTerm": "Standard Generalized Markup Language",
                "Acronym": "SGML",
                "Abbrev": "ISO 8879:1986",
                "GlossDef": {
                    "para": "A meta-markup language, used to create markup languages such as DocBook.",
                    "GlossSeeAlso": ["GML", "XML"]
                },
                "GlossSee": "markup"
            }
        }
    }
 }
}

What i need now is something to handle it. Something like the following: (it's just an example how i imagine the following steps to get it run ;) )

public class AnyClass{

    public AnyClass(String jsonString){
        GSONObject gobject = new GSONObject(jsonString);
        String title = gobject.getValueOf("title");
    }
}

Thanks for every help :)

Upvotes: 1

Views: 4261

Answers (3)

Raj
Raj

Reputation: 1965

To convert json string to Gson (Deserialization)

Use gson.fromJson();

Example using String class

Gson gson = new Gson();
String str = gson.fromJson("\"abc\"", String.class);

Example using user defined class

BagOfPrimitives obj = new BagOfPrimitives();
Gson gson = new Gson();
BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);

The google help page for Gson has good explanations.

https://sites.google.com/site/gson/gson-user-guide#TOC-Primitives-Examples

Object Example

class BagOfPrimitives {
  private int value1 = 1;
  private String value2 = "abc";
  private transient int value3 = 3;
  BagOfPrimitives() {
    // no-args constructor
  }
}

(Serialization)

BagOfPrimitives obj = new BagOfPrimitives();
Gson gson = new Gson();
String json = gson.toJson(obj);  
==> json is {"value1":1,"value2":"abc"}

Note that you can not serialize objects with circular references since that will result in infinite recursion.

(Deserialization)

BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);   
==> obj2 is just like obj

Finer Points with Objects

It is perfectly fine (and recommended) to use private fields 
There is no need to use any annotations to indicate a field is to be included for serialization and deserialization. All fields in the current class (and from all super classes) are included by default. 
If a field is marked transient, (by default) it is ignored and not included in the JSON serialization or deserialization.

This implementation handles nulls correctly

While serialization, a null field is skipped from the output

While deserialization, a missing entry in JSON results in setting the corresponding field in the object to null

If a field is synthetic, it is ignored and not included in JSON serialization or deserialization

Fields corresponding to the outer classes in  inner classes, anonymous classes, and local classes are ignored and not included in serialization or deserialization

If you need more examples please look at the below links

http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/

http://filotechnologia.blogspot.it/2013/09/convert-java-object-from-json-gson-api.html

Upvotes: 0

Arjit
Arjit

Reputation: 3456

Try this

JSONParser parser = new JSONParser();
JSONObject json = (JSONObject) parser.parse(jsonString);

Using gson

Gson gson = new Gson();
gson.fromJson(jsonString, YourBean.class);

Upvotes: 3

Ashwinee K Jha
Ashwinee K Jha

Reputation: 9307

You can use javax.json package for processing json data. For details see https://docs.oracle.com/javaee/7/tutorial/jsonp002.htm

Upvotes: 0

Related Questions