[ACCEPTED]-Convert JSON to POJO-jersey

Accepted answer
Score: 10

To convert between Java and JSON, there 10 are a good number of APIs available to choose 9 from.

You could "manually" step 8 through the JSON components and extract 7 values to populate Java objects, or you 6 could use a JSON-to-Java binding API to 5 take care of many low-level mapping concerns.

Jackson is 4 such an API. It's easy to use and provides 3 probably the fullest set of API features 2 to address common issues and customizations. StackOverflow.com 1 has many examples of how to use it.

Score: 1

As an option, you can check JSON Simple.

0

Score: 0

You can also use JaxB binding which would 17 handle conversion to and from for you. Its 16 part of Java SE so no jar downloads required.However, you 15 would need to write a pojo class with all 14 the attributes your json object will return 13 and accessor methods to assess them. Then 12 you would add a XmlRootElement annotation 11 on that class this will enable jaxb to convert 10 to and from json where necessary. Example:

POJO

 @XmlRootElement
public class User
{
    private String name;

   public void setName (String name)
   {
      this.name = name;
   }

   public String getName ()
   {
       return name;
   }

}

Jersey 9 service

 @GET
 @Produces (MediaType.APPLICATION_JSON)
 public User getUser ()
 {
     User user = new User ();
     user.setName ("John Doe");
     return user;
 }

This would convert the User POJO 8 object and return it has the media type 7 specified in this example JSON. You can 6 even return it with a Response object. Example:

  @GET
 @Produces (MediaType.APPLICATION_JSON)
 public Response getUser ()
 {
     User user = new User ();
     user.setName ("John Doe");
     return Response.status (Status.OK).entity (user).build ();
 } 

This 5 returns a Response object with code 200 4 (Success) along with User JSON object. [NOTE] This 3 approach is preferred cause It provide the 2 user who invokes your web service information 1 about the status of the transaction or service

Score: 0

We can also make use of below given dependency 6 and plugin in your pom file.

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>1.7.1</version>
</dependency>

<plugin>
    <groupId>com.googlecode.jsonschema2pojo</groupId>
    <artifactId>jsonschema2pojo-maven-plugin</artifactId>
    <version>0.3.7</version>
    <configuration>
        <sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
        <targetPackage>com.example.types</targetPackage>
    </configuration>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
</plugin>

With the use 5 of these you can generate POJO's as per 4 your JSON Schema and then make use of code 3 given below to populate request JSON object 2 via src object specified as parameter to 1 gson.toJson(Object src):

Gson gson = new GsonBuilder().create();
String payloadStr = gson.toJson(data.getMerchant().getStakeholder_list());
Score: 0

Was looking for the same and since others 4 require you to add annotations or write/generate 3 getter and setter methods for your variables, I 2 wrote my own codec to deal with JSON<->POJO 1 transformations using reflections.

See here: http://homac.cakelab.org/projects/org.cakelab.json

More Related questions