java generates complex nested json

Posted by Modernvox on Wed, 01 Apr 2020 13:23:40 +0200

In the past, I didn't touch the complicated nested json data, but I returned some simple json data. Recently, I learned some knowledge about JSONObject and JSONArray.

Transformation relationship between json object and array of json objects


var jsonStr  = "{\"userId\":\"001\"}";  // json object string
var jsonArryStr = "[{\"userId\":\"001\"},{\"userId\":\"002\"}]"; // json array string
var jsonObj  = JSON.parse(jsonStr);     // String to json object
var jsonArry = JSON.parse(jsonArryStr); // String to json array
var jsonStr  = JSON.stringify(jsonObj); // json object to string
var jsonArryStr=JSON.stringify(jsonArry);// json array to string
alert(jsonStr);
alert(jsonArryStr);
alert(jsonObj.userId);
alert(jsonArry[0]['userId']);

In this part, I will focus on the generation of complex nested json. Here is the json data I generated

{github ': [{"g'u name": "laboratory website"}, {"g'u name": "new system"}, {"g'u name": "contact us"}], "project": [{"P'u name": "intelligent four rotor UAV"}, {"P'u name": "claw intelligent pet Pendant"}, {"P'u name": "urban parking intelligent guidance system"}], "skills": [{"address": "FZ332", "person": "Zhang Zhen", ”Subject ":" regular expression "," time ":" 2018-02-24 17:33:03.0 "}, {" address ":" FZ332 "," person ":" chengdabao "," subject ":" search algorithm "," time ":" 2018-02-22 17:33:07.0 "}, {" address ":" FZ332 "," person ":" Zhou Hao "," subject ":" Huffman tree "," time ":" 2018-02-28 17:12:16.0 "}]," status: 0}]

I've read other blogs about the generation of json in the third middle school. I feel that the second way is to use Map collection.

/*
         * In the map mode, first put the required data into the map collection,
         * Then put all the sets into the List set
         * List Collections can be transformed into json
         */
Map<String,Object> map=new HashMap<String,Object>();

List<Map> ttmaps=new ArrayList<>();
        for(Technology tt:technology) {
            //json.put(String.valueOf(tt.getT_id()), tt);
            Map<String,Object> tts=new HashMap<String,Object>();
            tts.put("subject", tt.getSubject());
            tts.put("person", tt.getPerson());
            tts.put("address", tt.getAddress());
            tts.put("time", tt.getTime());
            //System.out.println(tt.getSubject()+" ");
            ttmaps.add(tts);
            //tts.clear();
        }
        map.put("skills", ttmaps);
        System.out.println("Collection Map Establish json object:" + new JSONObject(map));
        try {
            ResponseUtil.write(response, new JSONObject(map));
        }catch(Exception e) {
            e.printStackTrace();
        }   

The other two ways are to use strings and bean s

/*
First, create the json object, put the required fields in the json, and add all the json objects to the
List Finally, the transformation of List and json is completed in the collection
*/
 static void StringCreateJson(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name","Fuck Wang");

        //Fans are an array, which is actually a nested json
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("name","Xiao Wang");
        jsonObject1.put("age",7);

        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("name","Little fuck");
        jsonObject2.put("age",10);

        //It can be seen from this that in fact, list and json are also mutually transformed
        List<JSONObject> jsonObjects = new ArrayList<JSONObject>();
        jsonObjects.add(jsonObject1);
        jsonObjects.add(jsonObject2);
        jsonObject.put("fans",jsonObjects);

        System.out.println("jsonObject Create directly json:" + jsonObject);
    }
/*
The bean method used is actually the same as the two above, except that the setter method is used to assign values, and the new JSONObject(actor) is created and generated at last
*/
 //Third, it is also quite common to use bean transformation. (here, map is used as the child json. If you have to create a complex bean object, it is recommended to use Gjson operation.)
    static void beanCreateJson(){
        Actor actor = new Actor();
        actor.setName("Fuck Wang");
        Map<String,Object> map1 = new HashMap<String,Object>();
        map1.put("name","Xiao Wang");
        map1.put("age",7);
        Map<String,Object> map2 = new HashMap<String,Object>();
        map2.put("name","Little fuck");
        map2.put("age",10);
        List<Map> maps = new ArrayList<Map>();
        maps.add(map1);
        maps.add(map2);
        actor.setFans(maps);
        System.out.println("java bean Establish json object:" + new JSONObject(actor));
    }
/*
java bean
*/

public class Actor {
    private String name;
    private List<Map> fans;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Map> getFans() {
        return fans;
    }

    public void setFans(List<Map> fans) {
        this.fans = fans;
    }

After understanding the blog written by others, I can realize it according to my own ideas, which is also a kind of mastery and learning for me.

Topics: JSON Java github