前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JSONObject详解「建议收藏」

JSONObject详解「建议收藏」

作者头像
全栈程序员站长
发布2022-08-02 13:35:45
7150
发布2022-08-02 13:35:45
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

JSONObject只是一种数据结构,可以理解为JSON格式的数据结构(key-value 结构),可以使用put方法给json对象添加元素。JSONObject可以很方便的转换成字符串,也可以很方便的把其他对象转换成JSONObject对象。

pom:

代码语言:javascript
复制
 <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.28</version>
        </dependency>

1.通过原生生成json数据格式。

代码语言:javascript
复制
JSONObject zhangsan = new JSONObject();
        try {
            //添加
            zhangsan.put("name", "张三");
            zhangsan.put("age", 18.4);
            zhangsan.put("birthday", "1900-20-03");
            zhangsan.put("majar", new String[] {"哈哈","嘿嘿"});
            zhangsan.put("null", null);
            zhangsan.put("house", false);
            System.out.println(zhangsan.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

2.通过hashMap数据结构生成

代码语言:javascript
复制
     HashMap<String, Object> zhangsan = new HashMap<>();
        
        zhangsan.put("name", "张三");
        zhangsan.put("age", 18.4);
        zhangsan.put("birthday", "1900-20-03");
        zhangsan.put("majar", new String[] {"哈哈","嘿嘿"});
        zhangsan.put("null", null);
        zhangsan.put("house", false);
        System.out.println(new JSONObject(zhangsan).toString());

3.通过实体生成

代码语言:javascript
复制
        Student student = new Student();
        student.setId(1);
        student.setAge("20");
        student.setName("张三");
        //生成json格式
        System.out.println(JSON.toJSON(student));
        //对象转成string
        String stuString = JSONObject.toJSONString(student);

4.JSON字符串转换成JSON对象

代码语言:javascript
复制
String studentString = "{\"id\":1,\"age\":2,\"name\":\"zhang\"}";

//JSON字符串转换成JSON对象
JSONObject jsonObject1 = JSONObject.parseObject(stuString);

System.out.println(jsonObject1);

5.list对象转listJson

代码语言:javascript
复制
ArrayList<Student> studentLsit = new ArrayList<>();
        Student student1 = new Student();
        student1.setId(1);
        student1.setAge("20");
        student1.setName("asdasdasd");

        studentLsit.add(student1);

        Student student2 = new Student();
        student2.setId(2);
        student2.setAge("20");
        student2.setName("aaaa:;aaa");

        studentLsit.add(student2);

        //list转json字符串
        String string = JSON.toJSON(studentLsit).toString();
        System.out.println(string);

        //json字符串转listJson格式
        JSONArray jsonArray = JSONObject.parseArray(string);

        System.out.println(jsonArray);

阿里的json很好用,还有一个谷歌Gson也不错。有兴趣的可以看一看

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/125293.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年4月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档