JSON to Objects in java? JSON to Objects in java? json json

JSON to Objects in java?


Jackson and XStream have the ability to map json to POJOs.


Do you want the .java source file to be generated for you? Or to map exiting java beans to JSON objects?

If the former, there is no such a library ( that I'm aware of ) if the later, Google GSON is exactly what you need.

From the samples:

class BagOfPrimitives {    public 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); System.out.println( json );

Prints

{"value1":1,"value2":"abc"}

( Deserialization )

BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);   System.out.println( obj2.value1 ) ; // value1 is 1


I think the Jackson data mapper can do what you need. It can serialize/deserialize a real Java object into a Json tree.

But others API should also work :

  • Sojo
  • FlexJSON
  • Gson