目录
Java中使用
首先导入fastjson的包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
将对象转换为json字符串
JSON.toJSONString(map)
将json字符串转换为json对象
JSON.parseObject(JSON.toJSONString(map));
示例:
public static void main(String[] args) {
try {
HashMap<String, String> map = new HashMap<>();
map.put("sasa","sasa");
map.put("sas","11");
map.put("fdf","qq");
System.out.println("map是"+map);
System.out.println("json字符串是"+JSON.toJSONString(map));
String s = JSON.toJSONString(map);
JSONObject jsonObject = JSON.parseObject(s);
System.out.println("json对象是"+jsonObject);
System.out.println("获取json中的某个属性是"+jsonObject.get("sas"));
} catch (Exception e) {
e.printStackTrace();
}
}
结果输出:
map是{sas=11, sasa=sasa, fdf=qq}
json字符串是{"sas":"11","sasa":"sasa","fdf":"qq"}
json对象是{"sas":"11","sasa":"sasa","fdf":"qq"}
获取json中的某个属性是11
JS中使用
看下面这篇文章,写的非常棒!!!