Preface
JSON is the abbreviation of JavaScript Object Notation, a lightweight form of data exchange, an alternative to XML, and is smaller, faster and easier to parse than XML. Because JSON uses JavaScript syntax when describing objects, it is language and platform-independent, and many JSON parsers and class libraries have been developed over the years.
JSON has the following forms:
An object is an unordered collection of "'name/value' pairs'". An object starts with "{" (open bracket) and ends with "}" (close bracket). Each "name" is followed by a ":" (colon); "'name/value' pair" is separated by "," (comma).
An ordered list of values. In most languages, it is implemented as an array, a vector, a list, a sequence.
These are common data structures. Currently, most programming languages support them in some form. This makes it possible to exchange data in the same format between various programming languages.
For obtaining data from the URLs of other servers, we generally transmit JSON data. For example, Server B needs to obtain paging information from the URLs of Server A. After obtaining JSON characters, if it is convenient and quick to operate, it must be converted into your own Java object.
Here I have tried three third-party JSON to convert, a Google GSON. This one seems to be unable to convert successfully. It keeps reporting a json format error, but the verification has been correct and the attempt failed. The second type is net.sf.json, which means no errors are reported, but the value of the object is always empty. Only json-simple can successfully convert objects
Parse json
First of all, we need to parse json to see if json is a standard json format. We can view json in many online formats on the Internet. Generally, we change the soup and not the medicine when parsing json as an object.
The first: I need to know the json structure. Here I will start server A and get a string of json data from its url.
Throwing the copy json data into the formatting tool can clearly see the structure:
{ "eventList": [ { "id": 1030, "eventId": "1508779115818499700", "createTime": 1508779115000, "endTime": 1508865480000, "eventContent": "123", "openid": "o4eWZ06xzHW6mc2gMZMYVDEtx1zk" }, { "id": 1029, "eventId": "1508382907251660498", "createTime": 1508382906000, "endTime": 1508383020000, "eventContent": "ahhhhh", "openid": "o4eWZ06xzHW6mc2gMZMYVDEtx1zk" }, { "id": 1028, "eventId": "1508308708414580378", "createTime": 1508308721000, "endTime": 1508309040000, "eventContent": "30", "openid": "o4eWZ06xzHW6mc2gMZMYVDEtx1zk" }, { "id": 1027, "eventId": "1508308463330664814", "createTime": 1508308480000, "endTime": 1508308560000, "eventContent": "28", "openid": "o4eWZ06xzHW6mc2gMZMYVDEtx1zk" }, { "id": 1026, "eventId": "1508308463017144213", "createTime": 1508308479000, "endTime": 1508308560000, "eventContent": "27", "openid": "o4eWZ06xzHW6mc2gMZMYVDEtx1zk" } ], "page": "1", "size": "5", "isHasPage": "1"}Second: Create corresponding entity classes based on the structure
import com.yhn.consumer.entity.Event;import lombok.Data;import org.json.simple.JSONObject;import java.io.Serializable;import java.util.List;@Datapublic class EventVO implements Serializable{ private List<Event> eventList; private String page; private String size; private String isHasPage; public EventVO(JSONObject object){ //Constructor method assigns value to the object eventList = (List<Event>)object.get("eventList"); page = (String)object.get("page"); size = (String)object.get("size"); isHasPage = (String)object.get("isHasPage"); }} Because it is an embedded structure, the following is the Event class in private List<Event> eventList ;
import lombok.Data;import org.hibernate.annotations.DynamicUpdate;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import java.io.Serializable;import java.util.Date;@Entity@Data@DynamicUpdate //Automatic update time field public class Event implements Serializable { //This is the Event in List in EventVO @Id @GeneratedValue private Integer id; //Sorting function private String eventId; //Event id private Date createTime; //Creation time private Date endTime; //Update time private String eventContent; //Remind event content private String openid; //User id} Test class
Next, the structure is also written. You will write a unit test on Server B to see if the value can be assigned successfully.
@Test public void clientDemo()throws Exception { StringBuilder json = new StringBuilder(); URL urlObject = new URL("http://127.0.0.1/meetingSign/test/server?openid=o4eWZ06xzHW6mc2gMZMYVDEtx1zk&page=2&size=5"); //Specify URL HttpURLConnection uc = (HttpURLConnection) urlObject .openConnection(); //Enable connection BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream(), "utf-8")); String inputLine = null; while ((inputLine = in.readLine()) != null) { json.append(inputLine); //Split json } in.close(); uc.disconnect(); System.out.println(json); //See if you get it. //Remove possible UTF-8, etc., because it may contain Bom header files. String result = formatString(json.toString()); JSONParser parser=new JSONParser(); org.json.simple.JSONObject data = new org.json.simple.JSONObject(); data = (org.json.simple.JSONObject) parser.parse(result); EventVO eventVO = new EventVO(data); //Pass the value into log.info("eventVO={}",eventVO); } /** * Remove the bomb header*/ public static String formatString(String s) { if (s != null) { s = s.replaceAll("/ufeff", ""); } return s; } Test results
It's assigned a value to the object
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.