Home > Java > javaTutorial > How to convert Map and JSON data to and from each other in Java?

How to convert Map and JSON data to and from each other in Java?

PHPz
Release: 2023-04-27 15:40:08
forward
3461 people have browsed it

map to JSON string

package com.hanfan.test;// HANFAN自动读取PC名字,也可以写死这个名字

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author HANFAN
 * @Date 2022/4/21 16:01
 * @Version 1.0
 */

public class mapAndJsonCast {

    public static void main(String[] args) {

        /* map转化JSON字符串常用 */
        Map testMap = new HashMap<>();
        testMap.put("str1","fu");
        testMap.put("str2","ck");
        String str = JSON.toJSONString(testMap);
        System.out.println("testMap的值:" + testMap);

//      打印的结果是:       testMap的值:{str1=fu, str2=ck}
    }
}
Copy after login

JSON string to JSON object

        /* JSON字符串转JSON对象常用*/
        String jsonStr = "{\"str3\":\"zhangsan\",\"str4\":\"lisi\",\"str5\":\"wangwu\",\"str6\":\"maliu\"}";
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        System.out.println("jsonObject:" + jsonObject);
//      打印的结果是:       jsonObject:{"str5":"wangwu","str6":"maliu","str3":"zhangsan","str4":"lisi"}
Copy after login

Map to JSON object

It can be seen from the above two conversions that map is converted to JSON first String, and then convert the JSON string into a JSON object

        /*Map转JSON对象常用 */
        JSONObject JSONObj = JSONObject.parseObject(JSON.toJSONString(testMap));
        System.out.println("JSONObj:" + JSONObj);
//      打印的结果是:        JSONObj:{"str1":"fu","str2":"ck"}
Copy after login

JSON string to Map

        /* JSON字符串转Map常用*/
        Map<String,Object> strMap =  JSONObject.parseObject(jsonStr);
        System.out.println("strMap:" + strMap);
//      打印的结果是:        strMap:{"str5":"wangwu","str6":"maliu","str3":"zhangsan","str4":"lisi"}
Copy after login

JSON object to Map

In fact, the json object is first converted into json characters String, json string and then map

        /*JSON对象转map常用*/
        Map<String,Object> jsonToMap =  JSONObject.parseObject(jsonObject.toJSONString());
        System.out.println("jsonToMap:"+jsonToMap);
//      打印的结果是:        jsonToMap:{"str5":"wangwu","str6":"maliu","str3":"zhangsan","str4":"lisi"}
Copy after login

JSON object to JSON string

        //JSON对象转JSON字符串
        String jsonToStr = jsonObject.toJSONString();
        System.out.println("jsonToStr=>"+jsonToStr);
//      打印的结果是:       jsonToStr=>{"str5":"wangwu","str6":"maliu","str3":"zhangsan","str4":"lisi"}
Copy after login

Based on the above conversion, it is not difficult to figure out the relationship between the three

How to convert Map and JSON data to and from each other in Java?

In order to facilitate memory, you can change your thinking.

When converting Map to jsonObject, Map is the subject, and JSON represents Map [abbreviation, JSON represents Map];

When jsonObject is converted to Map, jsonObject is the subject, and jsonObject still represents JSONObject [abbreviation, I represent myself]

The difference between Map and JSON, I only know is the difference between "=" and ":".

        /* map转化JSON字符串常用 */
        Map testMap = new HashMap<>();
        testMap.put("str1","fu");
        testMap.put("str2","ck");
        String str = JSON.toJSONString(testMap);
        System.out.println("testMap的值:" + testMap);

//      打印的结果是:       testMap的值:{str1=fu, str2=ck}
        /* JSON字符串转JSON对象常用*/
        String jsonStr = "{\"str3\":\"zhangsan\",\"str4\":\"lisi\",\"str5\":\"wangwu\",\"str6\":\"maliu\"}";
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        System.out.println("jsonObject:" + jsonObject);
//      打印的结果是:       jsonObject:{"str5":"wangwu","str6":"maliu","str3":"zhangsan","str4":"lisi"}
Copy after login

It can be seen from the printing results that Map is "=" and jsonObject is ":".

Which one to use depends on the actual situation.

IDEA function shortcut keys

Global search: Ctrl H

Search based on file name: Ctrl Shift R/T

Search in a certain category: Ctrl F

Add Comment [/**/]: Ctrl Shift /[Use twice to release the comment]

[//] : Ctrl /[Use twice to release the comment]

The above is the detailed content of How to convert Map and JSON data to and from each other in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template