站三界导航
首页 json数据json数据包括JSONObject和JSONArray

json数据包括JSONObject和JSONArray

  • json数据
  • 来源:站三界导航
  • 37阅读
  • 2023-03-30

json数据包括JSONObject和JSONArray。

即json包括json对象和json数组,json对象中有2种数据结构,对象结构和数组结构。

JSONObject的数据格式:{“名称”:“值”,“名称”:[数组],……}

JSONArray的数据格式:[{“名称”:“值”},{“名称”:[数组]},{“名称”:“值”},……]
JSONArray中包含1个或多个JSONObject

json数据,相当于键值对,下面我们来看一个复杂的json数据

{
    "features":[
        {
            "id":1,
            "geometry":{
                "type":"Point",
                "coordinates":[
                    533958.52189999819,
                    3123489.1460000016
                ]
            }
        },
       {
            "id":1,
            "geometry":{
                "type":"Point",
                "coordinates":[
                    533958.52189999819,
                    3123489.1460000016
                ]
            }
        },
    ]
}

关于java处理json的框架库有Jackson,Gson和FastJson等,我目前只用过fastjson库,Jackson是SpringBoot默认的json解析,听说Gson功能上好,FastJson不建议生产使用,也听过Gson和FastJson两种并行使用。

下面的代码我们用的是fastjson库,Fastjson是一个Java语言编写的JSON处理器,由阿里巴巴公司开发。fastjson是目前java语言中最快的json库,比自称最快的jackson速度要快。

一.生成json数据

1.通过原生生成json数据

//json对象
JSONObject j1 = new JSONObject();
j1.put("type", "Point");    //对象结构
j1.put("coordinates", new double[]{533958.52189999819, 3123489.1460000016});//数组结构

//json数组
JSONArray jsonArray1 = new JSONArray();
jsonArray1.add(j1);

2.json字符串生成json数据

//生成json对象
String stuString = "{\"id\":1,\"age\":2,\"name\":\"zhang\"}";//引号需要转义字符
JSONObject jsonObject1 = JSON.parseObject(stuString);

//生成json数组
String stuString2 = "[{\"id\":1,\"age\":2,\"name\":\"zhang\"}]";//引号需要转义字符
JSONArray jsonArray1 = JSON.parseArray(stuString2);

//上面的JSON换成JSONObject或者JSONArray都可以因为parseObject方法和parseArray方法在JSON中,而JSONObject和JSONArray都继承自JSON

3.json字符串生成对应的类

Student stu1= JSON.parseObject(stuString,Student.class);
List<Student>  stu2= JSON.parseArray(studentLsit,Student.class);
//字符串stuString中存放的是json对象,字符串studentLsit中存放的json数组

4.读取数据

//JSON实际上也是键值对("key":"value")
//key 必须是字符串,value 可以是合法的 JSON 数据类型(字符串, 数字, 对象, 数组, 布尔值或 null)
int a1= js.getIntValue("s1");
long a3= js.getLongValue("s2");
String a2= js.getString("s3");
Object o= js.get("name");
JSONObject js1=js.getJSONObject("s4");
JSONArray js2=js.getJSONArray("s5");

5.通过实体生成json对象和json字符串

Student student = new Student();
student.setId(1);
student.setAge("20");
student.setName("张三");
JSONObject js= (JSONObject) JSON.toJSON(student);//通过实体生成json对象
String stuString = JSON.toJSONString(student);   //通过json类中的方法转换成字符串
String stuString2 = JSON.toJSON(student).toString();//通过Object中的方法转换成字符串
String stuString3 = js.toString();//其实就是调用JSON中的toJSONString方法

ArrayList<Student> studentLsit = new ArrayList<>();
studentLsit.add(student);
JSONArray js2= (JSONArray) JSON.toJSON(studentLsit);//通过实体数组生成json对象
String stuLsitString = JSON.toJSONString(studentLsit);//通过json类中的方法转换成字符串
String stuLsitString2 = JSON.toJSON(studentLsit).toString();//通过Object中的方法转换成字符串
String stuLsitString3 = js2.toString();//其实就是调用JSON中的toJSONString方法
//同理,JSON也可以换成JSONObject或者JSONArray

6.遍历json数组

//遍历json数组
        JSONArray jsonArray=JSONObject.parseArray(data);
        JSONObject object;
        if(jsonArray.size()>0){
            for(int i=0;i<jsonArray.size();++i){
                // 遍历 jsonarray 数组,把每一个对象转成 json 对象
                object =jsonArray.getJSONObject(i);
                String stuString = JSONObject.toJSONString(object);
            }
        }

7.判断是json对象还是json数组

    JSONObject json = JSONObject.parseObject(s1); //得到整个json串
    Object o=json.get("terrainEntity");        //获取到对应的json对象
        if(o instanceof JSONArray){    //判断是那个类
        JSONArray json2=json.getJSONArray("terrainEntity");
    }
        else if(o instanceof JSONObject){
        JSONObject json2=json.getJSONObject("terrainEntity");
    }
//instanceof关键字,是用来判断前面的那个是属于哪个类的

二.总结

根据最上面的json数据,可以看到上面的type和coordinates分别是json的对象结构和数据结构

直接用JSONObject创建即可。

然后就是创建嵌套的json数据

首先我们先要创建一个

            //首先创建一个JSONObject,因为是json的对象结构
            JSONObject jsonObject = new JSONObject();
            JSONArray jsonArray1 = new JSONArray();
            for(int i=0;i<2;++i) {
                //创建一个嵌套的json数据,
                JSONObject j2 = new JSONObject(); 
                j2.put("id", 1);
                JSONObject j21 = new JSONObject();
                j21.put("type", "Point");
                j21.put("coordinates", new double[]{533958.52189999819, 3123489.1460000016});
                j2.put("geometry", j21);
                //数组中,添加每一个json数据
                jsonArray1.add(j2);
            }
            jsonObject.put("features",jsonArray1);

fastjson的apihttps://www.w3cschool.cn/fastjson/fastjson-stream.html

使用fastjson在处理超大文本反序列化,具体是指,把从服务器端获取到的超大json文本数组(每一个json对象中的内容也很多很长,具体有好多键值对),然后我要把这个json数组转换成类对象的数组的时候,就会出错,具体处理方式,这个fastjson好像有超大JSON文本序列化和反序列化的方法,下次遇到了,可以看看学习一下,目前还不会用。

本文结束
本文来自投稿,不代表站三界导航立场,如若转载,请注明出处:https://www.zhansanjie.com/article/details/40375.html

版权声明:

1、本文系转载,版权归原作者所有,旨在传递信息,不代表看本站的观点和立场。

2、本站仅提供信息发布平台,不承担相关法律责任。

3、若侵犯您的版权或隐私,请联系本站管理员删除。

4、本文由会员转载自互联网,如果您是文章原创作者,请联系本站注明您的版权信息。

分享
站三界导航
本站声明:本站严格遵守国家相关法律规定,非正规网站一概不予收录。本站所有资料取之于互联网,任何公司或个人参考使用本资料请自辨真伪、后果自负,站三界导航不承担任何责任。在此特别感谢您对站三界导航的支持与厚爱。