package asd.we.utils;
import java.util.Map;
import net.sf.json.JSONObject;
public class StringUtil {
/**
* mapString {name=test,sex=nan,age=12}
* 需要json.jar及相关依赖包 下载地址:http://download.csdn.net/detail/wd4871/9180013
* map形式字符创 转 json字符串
* return {"name":"test","sex":"nan","age":"12"}
**/
public static String fromMSToJS(String mapString){
String result="{";
String strData=mapString.substring(mapString.indexOf("{")+1, mapString.lastIndexOf("}"));
String arr[]=strData.split(",");
for(int i=0;i<arr.length;i++){
result+=arr[i].replace("=", ":'")+"',";
}
String dataJson=result.substring(0, result.length()-1);
JSONObject obj = JSONObject.fromObject(dataJson+"}");
String dson=obj.toString();
return dson;
}
/**
* mapString {name=test,sex=nan,age=12}
* 需要json.jar及相关依赖包 下载地址:http://download.csdn.net/detail/wd4871/9180013
* map形式字符创 转 map对象
* return map对象
**/
public static Map<?, ?> fromSMToMapObejct(String mapString){
String result="{";
String strData=mapString.substring(mapString.indexOf("{")+1, mapString.lastIndexOf("}"));
String arr[]=strData.split(",");
for(int i=0;i<arr.length;i++){
result+=arr[i].replace("=", ":'")+"',";
}
String dataJson=result.substring(0, result.length()-1);
JSONObject obj = JSONObject.fromObject(dataJson+"}");
Map<?, ?> map = (Map<?, ?>)obj;
return map;
}
public static String getMapString (Map<?,?> map){
JSONObject obj = JSONObject.fromObject(map);
return obj.toString();
}
}
map字符串转json字符串,转map对象
最新推荐文章于 2024-06-25 00:00:00 发布