Json的理解

来源:互联网 发布:mysql if else 多条件 编辑:程序博客网 时间:2024/06/05 13:23
今天用到了Json发现还是要重写遍代码,才能记起来
 class TestJson {public static void main(String[] args) {TestJson te = new TestJson();te.jsonS();}public static  String jsonS(){JSONObject js = new JSONObject();List<Phone> list = new ArrayList<>();list.add(new Phone("MIUI", 1000.99));list.add(new Phone("Apple", 7000.99));HashMap<String, List<Phone>> map = new LinkedHashMap<>();map.put("Phone", list);//方法一 map的key为变量 list为数组内JSONObject obj = JSONObject.fromObject(map);System.out.println(obj.toString());//方法2 可以添加变量 map 的key为数组内的keyjs.put("Gzh", list);js.put("GuoF",map);System.out.println(js);//获取值 JSON结构 {变量:[{key:value,key:value},{key:value},{key:value}]} //中括号内是数组//JSONObject jsonObj = JSONObject.fromObject(obj.toString());//System.out.println(jsonObj);String array =obj.getString("Phone");System.out.println(array);JSONArray array2 =obj.getJSONArray("Phone");System.out.println("Phone:"+array2.getString(0));System.out.println("Phone:"+array2.getString(1));return obj.toString();//String phone  = js.getObject(1);}}

0 0