首页 > 其他分享 >JSON.parseObject与JSONObject.parseObject的区别

JSON.parseObject与JSONObject.parseObject的区别

时间:2022-11-24 11:34:33浏览次数:47  
标签:String JSONObject parseObject blog JSON import

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

 

标签:String,JSONObject,parseObject,blog,JSON,import
From: https://www.cnblogs.com/pandaly/p/16921254.html

相关文章

  • JsonResult向前端返回值,报错500
    1,问题原因因为返回信息为json对象,我在controller方法所在的入口类上,添加的注解是:@Controller而@Controller是不适合返回json内容的2,解决方法方法一:不影响其它方法......
  • 纯前端根据JSON数据导出excel
     本文是草稿,具体文章内容待完善,如急需实现功能,可以加我好友告诉你vx:programmer-duan配置excel表头    配置字段  ......
  • jsonpath 类的用法
    1importjson23#字典==>json4test_dict={"key1":"val1","key2":None,"key3":True,"key4":False}5new_json=json.dumps(test_dict)6print(type(new......
  • Filter,Listener,AJAX和JSON
    Filter是过滤器,就是在浏览器发送请求给服务器访问服务器资源时,需要先经过服务器中的filter过滤器,filter放行了,请求才能到达资源哪里,常用于权限设置,比如要查看某个数据库中......
  • JWT( JSON Web Token —— JSON Web 令牌 )的学习笔记
    一、跨域认证的问题互联网服务离不开用户认证。一般流程是下面这样:1、用户向服务器发送用户名和密码。2、服务器验证通过后,在当前对话(session)里面保存相关数据,比如用......
  • bitconin-cli 私有链搭建及jsonrpc-api
    精通比特币第三章,讲了如何编译等最后搭建成功之后执行:bitcoind-regtest-daemon后台运行私有链,不加-regtest默认公有链,包括之后对私有链操作,加上-regtest即可如果为了以......
  • [译]Golang中JSON和结构体的组合使用
     原文地址:http://attilaolah.eu/2014/09/10/json-and-struct-composition-in-go/ 假设你正在把一个JSON对象解码为Go的结构体。该JSON来自不受你控制的服......
  • GJSON的使用
    GJSON的使用 简介什么是Gjson:GJSON是一个Golang包,它提供了一种快速,简单的方法来从json格式文档中获取值。它拥有比如单行检索,用"."符号来寻找路径......
  • Fast_JSON数据和JAVA对象之间的转换
    JSON数据和JAVA对象之间的转换 json字符串转java对象   java对象转json字符串  ......
  • Newtonsoft的高级玩法,让你的json字符串与众不同
    json一经出现就得到多很多开发员的青睐,数据传输直接取代了之前的xml格式,不过也确实非常好用。关于json的常用操作,可以参考这篇文章。今天要分享的是Newtonsoft这个类库对Js......