Detail view of JSONObject

First look at what methods it has used, and what role:

1.put (String key, Object value) method, setting the value of the object in JSONObject, during the set time worth, is the only key, if the same set key is worth the time and constantly, behind the retention value.

2.Object get (String key): Get value corresponding to the value of the object according JSONObject key value, the value obtained is of type Object, manually converted to the required data type

3.int size (): Gets the number of key-value pairs of objects JSONObject

4.boolean isEmpty (): determining whether the object is empty JSONObject

5.containsKey (Object key): determining whether it is necessary to key value

6.boolean containsValue (Object value): value is determined whether it is necessary to value

7.JSONObject getJSONObject (String key): if the value JSONObjct JSONObject object is a target, i.e., obtain the corresponding objects according to JSONObject Key;

8.JSONArray getJSONArray (String key): if JSONObject object value is a JSONObject array, both arrays according to obtain a corresponding key JSONObject;

9.Object remove (Object key): According to one key Clear key pair.

Since JSONObject is a map, it also has two unique method of map:

10.Set keySet (): Gets JSONObject the key, and place it in the collection Set

11.Set <Map.Entry <String, Object >> entrySet (): used when looping through, get a mapping between keys and values, is the internal interface Entry Map interface

String and string conversion:

12.toJSONString () / toString (): converts a string json object JSONObject

The method is mainly used these are listed below using the example of these methods:

package com.snake.test;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Author Snake
 * @Date 2019/11/24 14:21
 * @Version 1.0.0
 */


public class myTest {

    public static void main(String[] args) {

        Map<String, Object> map = new HashMap<>();
        map.put("username","snake");
        map.put("age","18");
        map.put("password","123456");
        JSONObject jsonObject = new JSONObject(map);
        //jsonObject:{"password":"123456","age":"18","username":"snake"}
        System.out.println("jsonObject:"+jsonObject);
        jsonObject.remove("age");
        //去除一个key-values后的jsonObject:{"password":"123456","username":"snake"}
        System.out.println("去除一个key-values后的jsonObject:"+jsonObject);
        Object put = jsonObject.put("username", "snake17");
        //jsonObject.put:snake
        System.out.println("jsonObject.put:"+put);
        Object put1 = jsonObject.put("username", "snake27");
        //第一次jsonObject.put:snake17
        System.out.println("第一次jsonObject.put:"+put1);
        //第二次jsonObject.put:snake17
        System.out.println("第二次jsonObject.put:"+put1);

        JSONObject jsonObject1 = jsonObject.fluentPut("username", "snake107");
        //jsonObject1.fluentPut:{"password":"123456","username":"snake107"}
        System.out.println("jsonObject1.fluentPut:"+jsonObject1);

        Set<Map.Entry<String, Object>> entries = jsonObject.entrySet();
        //set键值对关系:[password=123456, username=snake107]
        System.out.println("set键值对关系:"+entries);
        Map<String, Object> innerMap = jsonObject.getInnerMap();
        //map对象:{password=123456, username=snake107}
        System.out.println("map对象:"+innerMap);
        Set<String> strings = jsonObject.keySet();
        //key值:[password, username]
        System.out.println("key值:"+strings);
        User user = jsonObject.toJavaObject(User.class);
        //jsonObject转对象:User(username=snake107, age=null, password=123456)
        System.out.println("jsonObject转对象:"+user);
        String s = jsonObject.toJSONString();
        //jsonObject转jsonString{"password":"123456","username":"snake107"}
        System.out.println("jsonObject转jsonString"+s);
        Object parse = JSONObject.parse(s);
        //jsonString转object:{"password":"123456","username":"snake107"}
        System.out.println("jsonString转object:"+parse);
        User user1 = JSONObject.parseObject(s, User.class);
        //jsonString转对象:User(username=snake107, age=null, password=123456)
        System.out.println("jsonString转对象:"+user1);

        ArrayList<User> users = new ArrayList<>();
        users.add(user);
        Object usersObject = JSONArray.parse(JSONArray.toJSONString(users));
        //usersObject:[{"password":"123456","username":"snake107"}]
        System.out.println("usersObject:"+usersObject);
        String s1 = JSONObject.toJSONString(usersObject);
        ArrayList arrayList = JSONObject.parseObject(s1, users.getClass());
        //[{"password":"123456","username":"snake107"}]
        System.out.println(arrayList);
        JSONArray objects = JSONArray.parseArray(JSONArray.toJSONString(users));
        //jsonArray:[{"password":"123456","username":"snake107"}]
        System.out.println("jsonArray:"+objects);

    }
}

Guess you like

Origin www.cnblogs.com/snake107/p/11961120.html