javajson字符串map互转⽅法⼀ alibaba.fastjson (string to map):
st.demo;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
public class Test {
public static void main(String[] args){
String jsonStr =
"{\"a\":\"a1\",\"b\":\"b1\",\"c\":\"c1\",\"d\":\"d1\"}";
Map<String,Object> mapObj =
JSONObject.parseObject(jsonStr,Map.class);
}
}
⽅法⼆ fasterxml.jackson (string to map) :
st.demo;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test {
public static void main(String[] args){
String jsonStr =
"{\"a\":\"a1\",\"b\":\"b1\",\"c\":\"c1\",\"d\":\"d1\"}";
ObjectMapper mapper =new ObjectMapper();
paramsMap = null;
try{
Map<String, String> map =
}catch(Exception e){
logger.warn("{}|{}",requestsn,e);
}
}
}
map转string,⽅法⼀
st.demo;replaceall()
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
public class Test {
public static void main(String[] args){
try{
HashMap<String, Object> map =new HashMap<String, Object>();
map.put("a","a1");
map.put("b","b1");
String mapStr = JSONString(map);
}catch(Exception e){
logger.warn("{}|{}",requestsn,e);
}
}
}
map转string,⽅法⼆
st.demo;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
public class Test {
public static void main(String[] args){
try{
HashMap<String, Object> map =new HashMap<String, Object>();
map.put("a","a1");
map.put("b","b1");
//⽅法⼆转出来包含回车换⾏,⽤replaceAll("[\\s*\t\n\r]", "")去掉json字符串中的回车换⾏,⽅法⼀转出来不包含回车换⾏  String jsonStr = JSONString(map,true).replaceAll("[\\s*\t\n\r]","");
}catch(Exception e){
logger.warn("{}|{}",requestsn,e);
}
}
}
实体类对象转json字符串
st;
import com.alibaba.fastjson.JSON;
st.ResultMessage;
@Service
public class Test{
public static void main(String[] args){
ResultMessage zfresult =new ResultMessage("0000","success","0","ok","0","ok","","","");  String result = JSONString(zfresult);
System.out.println(result);
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。