Type conversion involving JSON (JSON, JSONARRAY, JSONOBJECT)

tags: java  json  

1, guide dependence

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

2, create a Pojo object ()

public class Myuser {
    private int id;
    private String username;
    private String userpwd;
    private String usersex;
    private Integer version;
    private Date createTime;
    private Date updateTime;
}

3. Initialization of the object "Boss" in the test method

        Myuser myuser1 = new Myuser();
        myuser1.setId(1);
        Myuser1.setUsername ("Boss");
        myuser1.setUsersex("male");
        myuser1.setUserpwd("123");

4, transform "boss"

JSON.toJSONString Convert "Boss" to JSON format String:

String receiverOfPOJO = JSON.toJSONString(myuser1);
System.out.println(receiverOfPOJO);

Output results:

{"ID": 1, "Username": "Boss", "UserPwd": "123", "Usersex": "Male"}

Plus: Why do you use myuser1.toString ()?

System.out.println(myuser1.toString());

Output

Myuser (id = 1, username = boss, userpwd = 123, usersex = Male, Version = null, createtime = null, updatetime = null)

Contrast discovery: toString () method does not meet the JSON format, and JSON converted characters will ignore the attributes of the NULL. For example, you will find that we have not assigned to Version, and you can't see the JSON string. .


 

JSONObject.parseObjectTransform the "boss" of the String type to a Myuser type (note that the Myuser.class is passed, telling ParseObject to convert the target type):

Myuser receiverOfJSONObject = JSONObject.parseObject(receiverOfPOJO, Myuser.class);
System.out.println(myuser1.equals(receiverOfJSONObject));

Output result

true

 

 

5, build "old two", generate a list object

        Myuser myuser2 = new Myuser();
        myuser2.setId(2);
                 Myuser2.setusername ("old two");
        myuser2.setUsersex("male");
        myuser2.setUserpwd("abc");

        List<Myuser> myusers = new ArrayList<>();
        myusers.add(myuser1);
        myusers.add(myuser2);

6, convert LIST

JSON.toJSONString Convert List objects to JSON format strings
String receiverOfList = JSON.toJSONString(myusers);
System.out.println(receiverOfList);
System.out.println(myusers.toString());

Output:

[{"ID": 1, "UserName": "UserPwd": "123", "Usersex": "Male"}, {"ID": 2, "Username": "Old 2", " UserPwd ":" ABC "," Usersex ":" Male "}]
 [Myuser (id = 1, username = boss, userpwd = 123, usersex = malec, version = null, createtime = null, updatetime = null), myuser (id = 2, username = old two, userpwd = ABC, usersex = Male , Version = null, createtime = null, updatetime = null)]

Still comparing the string of the JSON converted string and the TOSTRING method to discovery differences.

 

 

 

 JSONArray.parseArrayConvert JSON format List strings to list <user>

List<Myuser> receiverOfJSONList = JSONArray.parseArray(receiverOfList,Myuser.class);
System.out.println(receiverOfJSONList.equals(myusers));

Output:

true

 

 

Intelligent Recommendation

JSON analysis JSONOBJECT and JSONARRAY

First, application scenario 1. The java class in the background, such as solid classes, list, set, map to the JSON string to be sent to the front or other items 2. JSON string sent by the front or oth...

JSON, JSONOBJECT, JSONARRAY in Fastjson

content Introduction demo Introduction demo plant test   Output result     Refer...

Relationship between JSON, JSONObject, JSONArray, javabean, get value, and mutual conversion

PS: If you just want to see the method, slide directly backwards. JSON, JSONObject, and JSONArray are all things in the FastJson framework. The JSON protocol is easy to use and popular. There are many...

Fastjson example of json string JSONObject and JSONArray conversion operation

Console output: Initial jsonObject: {"age":35,"name":"Andy Lau", "some":[{"k1":"v1","k2":"v2"},{" K3":"v3...

Json-lib uses JSONObject and JSONArray

1. From Object to String To construct a JSONObject or JSONArray object with an Object object, then call its toString() method. (1) Example one (2) Example 2 2. From String to Object To construct a JSO...

More Recommendation

Convert json strings to JSONObject and JSONArray

//JSONObject String jsonMessage = “{“Language”: “88”, “Mathematics”: “78”, “Computer”: “99”}”; String value1 = null;...

Json automatically recognizes jsonObject and JSONArray

Sometimes you need to automatically determine whether the parsed json string is a jsonObject or a JSONArray. So you need to import the jar package of net.sf.json code show as below  ...

JSON, JSONObject and JSONArray in detail and their application

Words written in front: This article uses Ali open source JSON parsing libraryfastjson Demonstrate and explain. Why write this at the front of the article? Because when I checked many blogs before, th...

JSONObject and JSONArray parse JSON data

example Use JSONObject and JSONArray to parse JSON data This is to parse json data This is the corresponding value Imported dependencies...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top