当前位置:和仁网 >休闲 > 正文

java json字符串转换Map

2021-11-30 1

用JSONUtil.deserialize()就可以了,范例:
import org.apache.struts2.json.JSONUtil;

public class JsonToJava {
public static void main(String[] args) {
try {
String s = "{"D":["a","b"],"A":["a","b"],"B":["a","b"],"C":["a","b"]}";
Object o = JSONUtil.deserialize(s);
System.out.println(o.getClass() + ": " + o);

} catch (Exception e) {
e.printStackTrace(System.out);
}
}
}import org.apache.struts2.json.JSONUtil;

public class JsonToJava {

public static void main(String[] args) {
try {
String s = "{"D":["a","b"],"A":["a","b"],"B":["a","b"],"C":["a","b"]}";

Object o = JSONUtil.deserialize(s); //转化

System.out.println(o.getClass() + ": " + o); //输出转化结果
Object o1 = ((HashMap)o).get("A");
System.out.println("A = " + o1.getClass() + ": " + o1); // 输出转化后的HashMap中“A”属性

} catch (Exception e) {
e.printStackTrace(System.out);
}
}
}

输出结果:
class java.util.HashMap: {D=[a, b], A=[a, b], B=[a, b], C=[a, b]}
A = class java.util.ArrayList: [a, b]将JSON字符串转换为 HashMap Map
本周热门
本月热门