Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json - Java Object to Json

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

“MyResult” {
    “AccountID”: “12345”,
    "User" {
        "Name": "blah blah",
        "Email": “[email protected]”,
     },
     "Result" {
         "Course": “blah”,
         "Score": “10.0”
     }
 }
like image 891
user238021 Avatar asked Jun 12 '12 17:06

user238021


People also ask

How do I parse a JSON object in Java?

Converting Java object to JSON In it, create an object of the POJO class, set required values to it using the setter methods. Instantiate the ObjectMapper class. Invoke the writeValueAsString() method by passing the above created POJO object. Retrieve and print the obtained JSON.

How does spring boot convert object to JSON?

When Jackson is on the classpath an ObjectMapper bean is automatically configured. The spring-boot-starter-json is pulled with the spring-boot-starter-web . In Spring objects are automatically convered to JSON with the Jackson library. Spring can be configured to convert to XML as well.


2 Answers

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.


How should I structure my Java object?

Below is what your object model could look like. MOXy's JSON binding leverages JAXB annotations for mapping the domain model to JSON, so I have included those as well. JAXB implementations have default rules for mapping field/property names, but since your document differs from the default each field had to be annotated.

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;

}

User

package forum11001458;

import javax.xml.bind.annotation.XmlElement;

public class User {

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

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

}

Result

package forum11001458;

import javax.xml.bind.annotation.XmlElement;

public class Result {

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

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

}

What Json library can I use for this?

Below is how you can use MOXy to do the JSON binding:

jaxb.properties

To use MOXy as your JAXB provider you need to include a file called jaxb.properties with the following entry in the same package as your domain model:

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

Demo

Note how MOXy's JSON binding does not require any compile time dependencies. All the necessary APIs are available in Java SE 6. You can add the necessary supporting APIs if you are using 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/Output

{
   "MyResult" : {
      "AccountID" : "12345",
      "User" : {
         "Name" : "blah blah",
         "Email" : "[email protected]"
      },
      "Result" : {
         "Course" : "blah",
         "Score" : "10.0"
      }
   }
}
like image 181
bdoughan Avatar answered Sep 28 '22 02:09

bdoughan


Googles GSON is a really nice json lib. This is from the previous link and it basically outlines some of its functionality.

like image 21
John Kane Avatar answered Sep 28 '22 02:09

John Kane