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

In Java ( using json-simple ) I successfully parse the JSON string created in JavaScript using JSON.stringify. It looks like this:

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

This line stores the state of a custom JavaScript object, which I now want to reformulate as a pure Java class. Its not very good - first run into Java, coming from the background of C #. :-P

Currently, the object is in the form org.json.simple.JSONObject, because it is that json-simple is made from the JSONParser.parse () operation.

How can I apply 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; } 
+6
source share
5 answers

use the jackson library

  //create ObjectMapper instance ObjectMapper objectMapper = new ObjectMapper(); //convert json string to object Employee emp = objectMapper.readValue(jsonData, Employee.class); 
+7
source

JSON string for java class? you can try 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); 
+2
source

There are many libraries that do 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> 
+2
source

The easiest way, but be careful to import the appropriate library

 Gson gson= new Gson(); //JSONObject CustomObject obj= gson.fromJson(jsonObject.toString(),CustomObject.class); 
+2
source

UPDATE 1/9/2018

Several years have passed since this (now popular) question was asked. Although I still agree that there was a need for json-simple at the time, I think the SO community is better served by having a checkmark next to Jackson's solution. I do not accept my answer today; Jackson is very good!


I think this decent json simple library is a victim of poor documentation. If you are not using JSONParser (see the picture!), But instead use this JSONValue.parse () method, it will all look like this:

  //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 everyone who posted. The json-simple question was a question, and this is the only json-simple answer to date. Jackson REALLY looks smooth, and Amazon uses it in the SDK for Java too, sooo .... if its good enough for AWS ....

+1
source

Source: https://habr.com/ru/post/970870/


All Articles