前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java json对象转map_java引用对象

java json对象转map_java引用对象

作者头像
全栈程序员站长
发布2022-09-23 11:08:44
2.8K0
发布2022-09-23 11:08:44
举报
文章被收录于专栏:全栈程序员必看

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

JSON.parseObject :是将Json字符串转化为相应的对象;JSON.toJSONString :则是将对象转化为Json字符串。 JSON.toJSON(user2) :把Java对象 转 JSON对象 JSONObject jsonObject1 = (JSONObject) JSON.toJSON(user2); // {“sex”:“男”,“name”:“秦疆2号”,“age”:3}

代码语言:javascript
复制
public class fastJson {

    public static void main(String[] args) {

        //创建一个对象

        User user1 = new User("秦疆1号", 3, "男");
        User user2 = new User("秦疆2号", 3, "男");
        User user3 = new User("秦疆3号", 3, "男");
        User user4 = new User("秦疆4号", 3, "男");
        List<User> list = new ArrayList<User>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);

        Map<String,Object> map =new HashMap<>();
        map.put("user1",user1);
        map.put("user2",user2);
        map.put("user3",user3);

        //JSON.toJSONString则是将对象转化为Json字符串。
        String mapString = JSON.toJSONString(map);
        System.out.println("json 格式的map"+mapString);
        //json格式的map{"user1":{"age":3,"name":"秦疆1号","sex":"男"},"user2":{"age":3,"name":"秦疆2号","sex":"男"},"user3":{"age":3,"name":"秦疆3号","sex":"男"}}

        //JSON.parseObject,是将Json字符串转化为相应的对象;
        JSONObject jsonObjectMap = JSON.parseObject(mapString);
        System.out.println("jsonObjectMap:"+jsonObjectMap);
//jsonObjectMap:{"user1":{"sex":"男","name":"秦疆1号","age":3},"user2":{"sex":"男","name":"秦疆2号","age":3},"user3":{"sex":"男","name":"秦疆3号","age":3}}

        //把json对象转换成map对象
        Map map1 = JSON.parseObject(mapString, Map.class);
        Object user11 = map1.get("user1");
        System.out.println("user11"+user11);

        System.out.println("*******Java对象 转 JSON字符串*******");
        String str1 = JSON.toJSONString(list);
        System.out.println("JSON.toJSONString(list)==>"+str1);
        //打印结果JSON.toJSONString(list)==>[{"age":3,"name":"秦疆1号","sex":"男"},{"age":3,"name":"秦疆2号","sex":"男"},{"age":3,"name":"秦疆3号","sex":"男"},{"age":3,"name":"秦疆4号","sex":"男"}]

        String str2 = JSON.toJSONString(user1);
        System.out.println("JSON.toJSONString(user1)==>"+str2);
        //JSON.toJSONString(user1)==>{"age":3,"name":"秦疆1号","sex":"男"}
         //为啥不是“{"age":3,"name":"秦疆1号","sex":"男"}” ?

        System.out.println("\n****** JSON字符串 转 Java对象*******");
        User jp_user1=JSON.parseObject(str2,User.class);
        System.out.println("JSON.parseObject(str2,User.class)==>"+jp_user1);

        System.out.println("\n****** Java对象 转 JSON对象 ******");
        JSONObject jsonObject1 = (JSONObject) JSON.toJSON(user2);
        System.out.println("(JSONObject) JSON.toJSON(user2)==>"+jsonObject1);
        //{"sex":"男","name":"秦疆2号","age":3}

        System.out.println("\n****** JSON对象 转 Java对象 ******");
        User to_java_user = JSON.toJavaObject(jsonObject1, User.class);
        System.out.println("JSON.toJavaObject(jsonObject1, User.class)==>"+to_java_user);

    }
}

JSON.toJSONString中序列化空字符串遇到的坑:

1.由json字符串转换成Map对象 如json字符串:{“contend”:[{“bid”:“22”,“carid”:“0”},{“bid”:“22”,“carid”:“0”}],“result”:100,“total”:2}

下面直接附代码:

代码语言:javascript
复制
//json字符串
String jsondata="{\"contend\":[{\"bid\":\"22\",\"carid\":\"0\"},{\"bid\":\"22\",\"carid\":\"0\"}],\"result\":100,\"total\":2}";
JSONObject obj= JSON.parseObject(jsondata);
//map对象
Map<String, Object> data =new HashMap<>();
//循环转换
 Iterator it =obj.entrySet().iterator();
 while (it.hasNext()) {
       Map.Entry<String, Object> entry = (Entry<String, Object>) it.next();
       data.put(entry.getKey(), entry.getValue());
 }
System.out.println("map对象:"+data.toString());

下面是输出内容:

{total=2, contend=[{“carid”:“0”,“bid”:“22”},{“carid”:“0”,“bid”:“22”}], result=100}

2.由Map对象转换成json字符串

代码语言:javascript
复制
//map对象
Map<String, Object> data =new HashMap<>();
String x =JSONObject.toJSONString(data);
System.out.println("json字符串:"+x);

下面是输出内容:

{“total”:2,“result”:100,“contend”:[{“carid”:“0”,“bid”:“22”},{“carid”:“0”,“bid”:“22”}]}

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

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

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

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

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

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