JSON 的简单的学习和使用

什么是Json

  json 是以中国轻量级的数据交换格式。被广菲用于web开发。

json 有点

  简单,清晰,层次结构

  易于别惹阅读,编写,

  可以提高网络传输效率

简单的json

  {

    "name":"cxx"

    "age":"30"

    "area":"shanxi"

  }

json数据

  [  

    {

      "name":"cxx",

      "age":"30",

      "area":"shanxi"

    },{

      "anme":"gmm",

      "age":"29",

      "area":"shanxi"

    }

  ]

Json是一种宏观叫法,json对象是json的具体的实现,json数组是多个json对象的集合。

什么是JSONObject

  JsonObject 是根据JSON的形式在java中存在的对象映射,

  

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(DEFAULT_INITIAL_CAPACITY, false);

}

public JSONObject(Map<String, Object> map){

    if (map == null) {

    throw new IllegalArgumentException("map is null.");

}

    this.map = map;

}    

  可以看法哦JsonObject就是对HashMap的一层封装,并提供了个性化的方法,其实用方式和HashMap没有什么区别

JSONObject person = new JSONObject();

person.put("name", "马马马马马百万");

person.put("age", 25);

person.put("area", "山东菏泽");

JSONObject drug = new JSONObject();

drug.put("drugName", "盐酸丁卡因注射液");

drug.put("drugCode", "HXUDP000000000000MED0000342809");

drug.put("dosformName", "注射剂");

  什么是JSONArray 

是针对fastjson而言的

public class JSONArray extends JSON implements List<Object>, Cloneable, RandomAccess, Serializable {

    private static final long serialVersionUID = 1L;

    private final List<Object> list;

    protected transient Object relatedArray;

    protected transient Type componentType;

    public JSONArray(){

    this.list = new ArrayList<Object>();

}

    public JSONArray(List<Object> list){

    this.list = list;

}

public JSONArray(int initialCapacity){

this.list = new ArrayList<Object>(initialCapacity);

}

// 。。。省略余下部分

}

  其实用方式和list没有多大的区别

JSONObject person = new JSONObject();

person.put("name", "马马马马马百万");

JSONObject drug = new JSONObject();

drug.put("drugName", "盐酸丁卡因注射液");

JSONArray array = new JSONArray();

array.add(person);

array.add(drug);

 引用地址: https://baijiahao.baidu.com/s?id=1652966908554903975&wfr=spider&for=pc

 自己对json的理解

原文地址:https://www.cnblogs.com/dousil/p/12760571.html