哆啦的时光机
1.JavaScript中:var str1 = '{ "name": "cxh", "sex": "man" }';//JSON字符串var str2 = { "name": "cxh", "sex": "man" };//JSON对象var obj = JSON.parse(str1); //由JSON字符串转换为JSON对象alert(obj.name);var last = JSON.stringify(str2);;//将JSON对象转化为JSON字符(这个也经常用到,所有就贴出来了)alert(last);2.java中(以alibaba.fastjson为例)://1.json字符串的格式为 key基本类型数据String jsonStr1 = "{\"id\":1,\"name\":\"张三\",\"age\":20}";Map obj1 = (Map) JSON.parse(jsonStr1);System.out.println(obj1+"第一个的age="+obj1.get("age"));//2.json字符串的格式为 key对象类型数据String jsonStr2 = "{\"1\":{\"id\":1,\"name\":\"张三\",\"age\":20}," +"\"2\":{\"id\":2,\"name\":\"李四\",\"age\":50}}";Map obj2 = (Map) JSON.parse(jsonStr2);System.out.println(obj2+"第一个的age="+obj2.get("1").get("age"));//3.json字符串的格式为 key不规则数据String jsonStr3 = "{\"1\":{\"id\":1,\"name\":\"张三\",\"age\":20}," +"\"2\":{\"id\":2,\"name\":\"李四\",\"age\":50},\"total_age\":70}";Map obj3 = (Map) JSON.parse(jsonStr3);System.out.println(obj3+"第一个的age="+obj3.get("1").get("age"));System.out.println("total_age="+obj3.get("total_age"));