JSON简单总结
在项目里做接口模块,通信协议用的是JSON,做完之后总结一下:
1、如果是单个字段,转换成JSON以后也是单个字段
"name" --> "name"
2、如果是数组或者List,则变成[]的形式,用,分隔每一项
String[] str = new String[]{"1","2","3"};
转换后是["1","2","3"]
3、如果是Map或者对象,则变成{}的形式,用:分隔键和值,用,分隔每一项
Map<String,String> map = new HashMap<String,String>();
map.put("key","value");
map.put("key2","value2");
转换后是{"key":"value","key2":"value2"}
4、JSON的基本形式就是上面3种,掌握了这3种,任何形式都一样。
List<List>,转换后变成[[1,2,3],[4,5,6],[7,8,9]]
List<map>,转换后变成[{"k1":"v1","k2":"v2"},{"k3":"v3","k4":"v4","k5":"v5"},{"k6":"v6","k7":"v7"}]