Json - Java object for Json

I am very new to Json and my goal is to create a Json output from a Java bean. How should I structure my Java object? Should I have a class MyResult and User and Result as subclasses? What Json library can I use for this?

"MyResult" {
    "AccountID": "12345",
    "User" {
        "Name": "blah blah",
        "Email": "blah@blah.com",
     },
     "Result" {
         "Course": "blah",
         "Score": "10.0"
     }
 }
+5
source share
5 answers

Note. I am EclipseLink JAXB (MOXy) and a member of JAXB (JSR-222) .


How should I structure my Java object?

. MOXy JSON JAXB JSON, . JAXB /, , .

MyResult

package forum11001458;

import javax.xml.bind.annotation.*;

@XmlRootElement(name="MyResult")
public class MyResult {

    @XmlElement(name="AccountID")
    private String accountID;

    @XmlElement(name="User")
    private User user;

    @XmlElement(name="Result")
    private Result result;

}

package forum11001458;

import javax.xml.bind.annotation.XmlElement;

public class User {

    @XmlElement(name="Name")
    private String name;

    @XmlElement(name="Email")
    private String email;

}

package forum11001458;

import javax.xml.bind.annotation.XmlElement;

public class Result {

    @XmlElement(name="Course")
    private String course;

    @XmlElement(name="Score")
    private String score;

}

Json ?

, MOXy JSON:

jaxb.properties

MOXy JAXB, jaxb.properties , :

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Demo

, MOXy JSON - . API- Java SE 6. API, Java SE 5.

package forum11001458;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(MyResult.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setProperty("eclipselink.media-type", "application/json");
        File json = new File("src/forum11001458/input.json");
        Object myResult = unmarshaller.unmarshal(json);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty("eclipselink.media-type", "application/json");
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(myResult, System.out);
    }

}

input.json/

{
   "MyResult" : {
      "AccountID" : "12345",
      "User" : {
         "Name" : "blah blah",
         "Email" : "blah@blah.com"
      },
      "Result" : {
         "Course" : "blah",
         "Score" : "10.0"
      }
   }
}
+9

Googles GSON - json lib. .

+7
+5

, SO GSON. "" , .

EDIT: especially for Jackson, your example is very similar to the example they give for what they call Full Data Binding, you can read it here . By the way, although the announced 5 minutes required to read this document may be a little short, it gives a complete overview of the various ways Jackson is used. You will also notice that the above examples do not use annotations.

+1
source

Or GSON

Super light (no getters / settlements, no annotations or configurations).

class BagOfPrimitives {
  private int value1 = 1;
  private String value2 = "abc";
  private transient int value3 = 3;
}

BagOfPrimitives obj = new BagOfPrimitives();
Gson gson = new Gson();
String json = gson.toJson(obj); 

==> json is {"value1":1,"value2":"abc"}
+1
source

All Articles