zl程序教程

您现在的位置是:首页 >  后端

当前栏目

java json对象转map_java引用对象

2023-06-13 09:11:01 时间

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

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

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}

下面直接附代码:

//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字符串

//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