JSON.parseObject与JSONObject.parseObject的区别

根据源码显示:JSON是一个抽象类,JSON中有一个静态方法parseObject(String text),将text解析为一个JSONObject对象并返回;

JSONObject是一个继承自JSON的类,当调用JSONObject.parseObject(result)时,会直接调用父类的parseObject(String text)。

所以两者没什么区别,一个是用父类去调用父类自己的静态的parseObject(String text),一个是用子类去调用父类的静态parseObject(String text),两者调的是同一个方法。

JSONObject继承了JSON

public class JSONObject extends JSON implements Map<String, Object>, Cloneable, Serializable, InvocationHandler {
    private static final long serialVersionUID = 1L;
    private static final int DEFAULT_INITIAL_CAPACITY = 16;
    private final Map<String, Object> map;

    public JSONObject() {
        this(16, false);
    }

    public JSONObject(Map<String, Object> map) {
        if (map == null) {
            throw new IllegalArgumentException("map is null.");
        } else {
            this.map = map;
        }
    }

JSONObject的toJsonString方法

    public static String toJSONString(Object object) {
        return toJSONString(object, emptyFilters);
    }

直接调用了JSON的toJSONString方法

    public static String toJSONString(Object object, SerializeFilter[] filters, SerializerFeature... features) {
        return toJSONString(object, SerializeConfig.globalInstance, filters, (String)null, DEFAULT_GENERATE_FEATURE, features);
    }

parseObject同理

演示

自己的一个类

package com.daylywork.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import java.time.LocalDateTime;
import java.io.Serializable;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("m_blog")
public class Blog implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;

    private Long userId;

    private String title;

    private String description;

    private String content;

    private LocalDateTime created;

    private Integer status;


}

测试类

package com.daylywork.study;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.daylywork.entity.Blog;

public class MyJSONparseObject {
    public static void main(String[] args){
        Blog blog=new Blog();
        blog.setId(1l);
        blog.setContent("setContent");
        blog.setDescription("setDescription");
        blog.setStatus(0);
        blog.setTitle("setTitle");
        blog.setUserId(2L);
        String str = JSONObject.toJSONString(blog);
        System.out.println(str);
        String blogs = "{\"content\":\"setContent\",\"description\":\"setDescription\",\"id\":1,\"status\":0,\"title\":\"setTitle\",\"userId\":2}";
        Blog blogg = JSONObject.parseObject(blogs,Blog.class);
        System.out.println(blogg.getContent());
        String strtwo = JSON.toJSONString(blog);
        System.out.println(strtwo);
        Blog bloggg= JSON.parseObject(strtwo,Blog.class);
        System.out.println(bloggg.getContent());
    }
}

结果

{"content":"setContent","description":"setDescription","id":1,"status":0,"title":"setTitle","userId":2}
setContent
{"content":"setContent","description":"setDescription","id":1,"status":0,"title":"setTitle","userId":2}
setContent
GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 4 个月前
8c391e04 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐