{“name”:”jhon”,”condition”:[“relation”:”and/or”,”sex”:”###”,”age”:”###”] } 怎么写这样格式的JSON怎么才能写出来?jinwenf:这个字符串写到JAVA里面的时候会报错,意思是在[]中不能有“

来源:学生作业帮助网 编辑:作业帮 时间:2024/04/29 18:06:18

{“name”:”jhon”,”condition”:[“relation”:”and/or”,”sex”:”###”,”age”:”###”] } 怎么写这样格式的JSON怎么才能写出来?jinwenf:这个字符串写到JAVA里面的时候会报错,意思是在[]中不能有“
{“name”:”jhon”,”condition”:[“relation”:”and/or”,”sex”:”###”,”age”:”###”] } 怎么写
这样格式的JSON怎么才能写出来?
jinwenf:这个字符串写到JAVA里面的时候会报错,意思是在[]中不能有“:”.
lazysunboy:你的方法也有些问题,最后生成出来的是“{"condition":{"sex":"main","age":"23","relation":"and"},"name":"john"}”这个样子,我需要的是[]里面是键值对,不要{}
我现在也不知道这个JSON串到底对不对了...
kejiaweiren:也就是说其实我的这个格式是错误的,不是JSON的格式吧,我生成的也是{}包裹着的.

{“name”:”jhon”,”condition”:[“relation”:”and/or”,”sex”:”###”,”age”:”###”] } 怎么写这样格式的JSON怎么才能写出来?jinwenf:这个字符串写到JAVA里面的时候会报错,意思是在[]中不能有“
public class Test {
public static void main(String[] args) {
Map map = new HashMap();
map.put( "name","json" );
Map map2 = new HashMap();
map2.put("relation","and/or");
map2.put("sex","###");
map2.put("age","###");
map.put("condition",map2);
JSONObject jsonObject = JSONObject.fromObject( map );
System.out.println( jsonObject );
}
}
出来的是{"condition":{"sex":"###","age":"###","relation":"and/or"},"name":"json"}
这个是正确的