java map转json字符_Map转JSON字符串

2022-08-31 15:33:08 浏览数 (1)

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

[java]代码库package com.smartAnji.control.utils;

import java.util.HashMap;

public class MessageUtil {

public final static String TYPE = “type”;

public final static String DATA = “data”;

public final static String MESSAGE = “message”;

public final static String COORD = “coord”;

public final static String BEGIN = “begin”;

public final static String USER = “user”;

public static HashMap getMessage(String msg) {

HashMap map = new HashMap();

String msgString = msg.toString();

String m[] = msgString.split(“,”);

map.put(“fromName”, m[0]);

map.put(“toName”, m[1]);

map.put(“content”, m[2]);

map.put(“type”, m[3]);

return map;

}

public static String sendContent(String type, Object mapContent) {

Map userMap = new HashMap();

userMap.put(MessageUtil.TYPE, type);

userMap.put(MessageUtil.DATA, mapContent);

// Map转JSON字符串

Gson gson = new Gson();

String jsonMsg = gson.toJson(userMap);

return jsonMsg;

}

//public static HashMap getCoord(String msg){

//HashMap map = new HashMap();

//String[] msgString = msg.toString().split(“_”);

//map.put(“x”, msgString[0]);

//map.put(“x”, msgString[1]);

//return map;

//}

}

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

0 人点赞