JSON.parseObject与JSONObject.parseObject的区别

JSON和JSONObject

先看一下源码
JSON源码

public abstract class JSON implements JSONStreamAware, JSONAware {
public static JSONObject parseObject(String text) {
        Object obj = parse(text);
        if (obj instanceof JSONObject) {
            return (JSONObject)obj;
        } else {
            try {
                return (JSONObject)toJSON(obj);
            } catch (RuntimeException var3) {
                throw new JSONException("can not cast to JSONObject.", var3);
            }
        }
    }
}

 

 

JSONObject源码

public class JSONObject extends JSON implements Map<String, Object>, Cloneable, Serializable, InvocationHandler {
public static JSONObject parseObject(String text) {
        Object obj = parse(text);
        if (obj instanceof JSONObject) {
            return (JSONObject)obj;
        } else {
            try {
                return (JSONObject)toJSON(obj);
            } catch (RuntimeException var3) {
                throw new JSONException("can not cast to JSONObject.", var3);
            }
        }
    }
}

 

可以看出来JSONObject是继承JSON的,会直接调用父类的parseObject(String text)方法。

总结 两者调用parseObject方法是同一个方法。不存在区别

JSONObject和JSONArray的区别

JSONObject的数据是用{ }框起来的,相当于一个json 举例:

{ "id" : "123", "name" : "meng", "age" : "16", "address" : "北京"}

 

JSONArray的数据最外面是[ ] 框起来的,里面可以包括多个json。 举例:

[{ "id" : "123", "name" : "meng", "age" : "16", "address" : "北京"},
{ "id" : "456", "name" : "wang", "age" : "18", "address" : "保定"}]

 

这相当于里面包含了两个json数据

怎样获取JSONArray里面的数据

JSONArray jsonList = JSON.parseArray(jsonAarryList);

 

 

Demo

自己的一个类

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

 

posted @ 2022-11-24 11:21  pandaly  阅读(1464)  评论(0编辑  收藏  举报