简体   繁体   English

如何将JSON转换为Java对象

[英]How can i convert JSON to Java object

Hi I am having a JSON of following format 嗨,我有一个以下格式的JSON

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

where list1,list2,list3 are all javascript arrays. 其中list1,list2,list3都是javascript数组。

Now I am able to pass this to my controller(am using spring mvc) from a javascript file. 现在我可以从javascript文件传递给我的控制器(我使用spring mvc)。 Now I want to use the data in the JSON that am sending. 现在我想使用正在发送的JSON中的数据。 I want to map this to a model class and return it another jsp page. 我想将其映射到模型类并将其返回到另一个jsp页面。 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: 使用Gson ,首先需要创建一个表示JSON数据的类结构,这样就可以创建一个这样的类:

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 . 请注意,我使用类YourObject因为您没有指定数组包含的类型...例如,如果数组只包含字符串, YourObjectString替换YourObject If the arrays contain a different object you have to create a class representing the data in your JSON, such as: 如果数组包含不同的对象,则必须创建表示JSON中数据的类,例如:

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: 然后,为了实际解析您的JSON响应,您只需要:

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: 并且您的JSON数据将用于填充您的类结构,因此您可以访问这些字段,例如:

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. 我发现它是最容易为我的json结构工作的。

You can use the jackson library. 您可以使用jackson库。 see: http://jackson.codehaus.org/ 见: http//jackson.codehaus.org/

Here is an example from: http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/ 以下是一个示例: 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();

    }

  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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