How can I cast a JSONObject to a custom Java class?

我只是一个虾纸丫 提交于 2020-01-01 07:56:14

问题


In Java (using json-simple) I have successfully parsed a JSON string which was created in JavaScript using JSON.stringify. It looks like this:

{"teq":14567,"ver":1,"rev":1234,"cop":15678}

This string is storing the state of a custom JavaScript object which I now wish to re-constitute as a pure Java class. Its not going well - first foray into Java coming from a C# background. :-p

The object is currently in the form of a org.json.simple.JSONObject since that is what json-simple made from the JSONParser.parse() operation.

How can I cast this JSONObject to my new Java class? (the definition of which is below...)

public class MyCustomJSObject {
    public int teq;
    public int ver;
    public int rev;
    public int cop;
}

回答1:


use jackson library

    //create ObjectMapper instance
    ObjectMapper objectMapper = new ObjectMapper();

    //convert json string to object
    Employee emp = objectMapper.readValue(jsonData, Employee.class); 



回答2:


Add dependency from https://github.com/google/gson and use it like

Gson gson= new Gson();
CustomPOJOClass obj = gson.fromJson(jsonObject.toString(),CustomPOJOClass.class);



回答3:


There are lot of libraries that does this. This is my suggestion. Here you can find the library

import com.google.gson.Gson; 

and then do,

Gson gson = new Gson();  
Student student = gson.fromJson(jsonStringGoesHere, Student.class);  

Maven Dependency

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



回答4:


JSON string to Java Class ? you can trying fastjson:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.1.41</version>
</dependency>

and using java code:

MyCustomJSObject object = JSON.parseObject(jsonString,MyCustomJSObject.class); 



回答5:


UPDATE 1/9/2018

Some years have passed since this (now popular) question was asked. While I still agree that json-simple was my need at the time, upon reflection I think the SO community is better served by having the "checkmark" next to the Jackson solution. I am un-accepting my own answer today; Jackson is pretty great!


I think that this decent json-simple library is the victim of poor documentation. If you don't use the JSONParser (go figure!) but instead use this JSONValue.parse() method, it all works out like so:

    //JSONParser parser = new JSONParser(); // DON'T USE THIS
    Object obj = JSONValue.parse("A JSON string - array of objects: [{},{}] - goes here");
    JSONArray arrFilings = (JSONArray)obj;
    System.out.println("We can print one this way...");
    System.out.println(arrFilings.get(5) + "\n");

    System.out.println("We can enumerate the whole array...");
    for(Object objFiling : arrFilings){
        System.out.println(objFiling);
    }
    System.out.println("\n");

    System.out.println("We can access object properties this way...");
    for(Object objFiling : arrFilings){
        JSONObject o = (JSONObject)objFiling; // MUST cast to access .get()
        MyJSObject.fyq = o.get("fyq");
    }
    System.out.println("\n");

Thanks to all those who posted. Sticking with json-simple was the question and this is the only json-simple answer to-date. Jackson DOES look slick, and Amazon uses it in their SDK for Java too, sooo.... if its good enough for AWS....



来源:https://stackoverflow.com/questions/24231223/how-can-i-cast-a-jsonobject-to-a-custom-java-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!