json

常用json解析工具类

 

1.net.sf.json

               如果对象中包含对象 不能一次把json字符串 转成对象,要一次一次转

 

 

        User user = new User("bob",12);
        User user1 = new User(null,null);
        List<User> list = new ArrayList<>();
        list.add(user1);
        list.add(user);
        
        System.out.println(JSONObject.fromObject(user1));
        
        JSONArray jsonArray = JSONArray.fromObject(list);
        System.out.println(jsonArray.toString());
        
        Map<String, String> map = new HashMap<>();
        map.put("data", null);
        System.out.println(JSONObject.fromObject(map));

输出:
{"age":0,"name":""}
[{"age":0,"name":""},{"age":12,"name":"bob"}]
{"data":null}


2.com.fasterxml.jackson
官网
https://github.com/FasterXML/jackson-databind/

User user = new User("bob",12);
        User user1 = new User(null,null);
        List<User> list = new ArrayList<>();
        list.add(user1);
        list.add(user);
        
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            JsonNode jsonNode = objectMapper.readTree(objectMapper.writeValueAsString(list));
            System.out.println(jsonNode.toString());
            
            Map<String, String> map = new HashMap<>();
            map.put("data", null);
            System.out.println(objectMapper.writeValueAsString(map));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }    
    }


3 FastJson
JSONObject result = new JSONObject();
result.put("data", HelloUcfPO);
result.toJSONString();

Student student = JSON.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {});

ArrayList<Student> students = JSON.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {});

 

JSONObject.toJSONString(HelloUcfPO)

 


//json 转对象
        //HelloUcfPO po = JSONObject.parseObject(data,HelloUcfPO.class);
        //json 转list
        //List<HelloUcfPO> list = JSON.parseArray(jsonObject.toString(), HelloUcfPO.class);
//JSON字符串反序列化API


    // 将JSON字符串反序列化为JavaBean
    public static <T> T parseObject(String jsonStr, 
                                    Class<T> clazz, 
                                    Feature... features);

    // 将JSON字符串反序列化为JavaBean
    public static <T> T parseObject(byte[] jsonBytes,  // UTF-8格式的JSON字符串
                                    Class<T> clazz, 
                                    Feature... features);

    // 将JSON字符串反序列化为泛型类型的JavaBean
    public static <T> T parseObject(String text, 
                                    TypeReference<T> type, 
                                    Feature... features);

    // 将JSON字符串反序列为JSONObject
    public static JSONObject parseObject(String text);

 

序列化API


    // 将Java对象序列化为JSON字符串,支持各种各种Java基本类型和JavaBean
    public static String toJSONString(Object object, SerializerFeature... features);

    // 将Java对象序列化为JSON字符串,返回JSON字符串的utf-8 bytes
    public static byte[] toJSONBytes(Object object, SerializerFeature... features);

    // 将Java对象序列化为JSON字符串,写入到Writer中
    public static void writeJSONString(Writer writer,
                                       Object object,
                                       SerializerFeature... features);

    // 将Java对象序列化为JSON字符串,按UTF-8编码写入到OutputStream中
    public static final int writeJSONString(OutputStream os,
                                            Object object,
                                            SerializerFeature... features);

 

更多参考features
https://www.jianshu.com/p/eaeaa5dce258






posted @ 2019-04-03 15:01  jentary  阅读(131)  评论(0编辑  收藏  举报