简体   繁体   中英

How can i convert JSON to Java object

Hi I am having a JSON of following format

{
"elements":[
        list1,
        list2,
        list3
    ]
}

where list1,list2,list3 are all javascript arrays.

Now I am able to pass this to my controller(am using spring mvc) from a javascript file. Now I want to use the data in the JSON that am sending. I want to map this to a model class and return it another jsp page. I didn't create a model yet. how can i pull this off?

Please help. Thanks in advance.

使用GSON将您的JSON转换为java

YourModelClass obj= gson.fromJson(json, YourModelClass .class);   

Using Gson , you first need to create a class structure representing your JSON data, so you can create a class like this:

public class Response {
    private List<List<YourObject>> elements;
    //getter and setter
}

Note that I use class YourObject since you don't specify what type your arrays contain... If the arrays contain just strings for example, replace YourObject by String . If the arrays contain a different object you have to create a class representing the data in your JSON, such as:

public class YourObject {
    private String attribute1;
    private int attribute2;
    private boolean attribute3;
    //getters and setters
}

Then, in order to actually parse your JSON response, you just have to do:

Gson gson = new Gson();
Response response = gson.fromJson(yourJsonString, Response.class);

And your JSON data will be used to fill your class structure, so you can access the fields, for example:

String attribute1 = response.getElements().get(i).get(i).getAttribute1();

Hi I used the following code and its working great.

Gson gson = new Gson();
    JsonParser jsonParser = new JsonParser();
    JsonArray jsonArray = jsonParser.parse(this.plan).getAsJsonArray();
    ArrayList<PlanJson> planJsonList = new ArrayList<PlanJson>();
    for(JsonElement jsonElement:jsonArray)
    {
        System.out.println(jsonElement);
        PlanJson planJson = gson.fromJson(jsonElement, PlanJson.class);
        planJsonList.add(planJson);
    }

I found it to be the most easiest to work out for my json structure.

You can use the jackson library. see: http://jackson.codehaus.org/

Here is an example from: http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/

package com.mkyong.core;

import java.io.File;
import java.io.IOException;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class JacksonExample {
    public static void main(String[] args) {

    ObjectMapper mapper = new ObjectMapper();

    try {

        // read from file, convert it to user class
        User user = mapper.readValue(new File("c:\\user.json"), User.class);

        // display to console
        System.out.println(user);

    } catch (JsonGenerationException e) {

        e.printStackTrace();

    } catch (JsonMappingException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    }

  }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM