官方www.json.org
<html>
<script>
var my_json='{FBI:[{name:"rose",age:"25"},{name:"jack",age:"23"}],NBA:[{name:"tom",sex:"man"},{name:"jack",sex:"women"}]}';
var my_object=eval('('+my_json+')');
document.writeln(my_object.FBI[1].name + my_object.FBI[1].age);
</script>
<body>
</body>
</html>
页面显示
jack23
json.js 从此下载 http://www.json.org/json.js<html>
<script type="text/JavaScript" src="json.js" ></script>
<script type="text/javascript">
var my_object={FBI:[{name:"rose",age:"25"},{name:"jack",age:"25"}],NBA:[{name:"tom",sex:"man"},{name:"jack",sex:"women"}]};
document.writeln("xx" + my_object.toJSONString());
</script>
<body>
test
</body>
</html>
页面显示 xx{"FBI":[{"name":"rose","age":"25"},{"name":"jack","age":"25"}],"NBA":[{"name":"tom","sex":"man"},{"name":"jack","sex":"women"}]} test
详细参考
http://www.json.org/java/index.html
- import <a href="http://lib.csdn.net/base/17" class='replace_word' title="Java EE知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>.util.ArrayList;
-
- import org.json.JSONArray;
- import org.json.JSONObject;
-
- public class Test {
-
- public static void main(String[] args) throws Exception {
- ArrayList<Area> areas = new ArrayList<Area>();
- areas.add(new Test.Area("福建", "福州"));
- areas.add(new Test.Area("福建", "厦门"));
- areas.add(new Test.Area("福建", "泉州"));
- areas.add(new Test.Area("四川", "成都"));
- areas.add(new Test.Area("四川", "攀枝花"));
- areas.add(new Test.Area("上海", "浦东"));
- JSONObject jsonObject = new JSONObject();
- for (Test.Area area : areas) {
- if (jsonObject.has(area.province)) {
- JSONArray jsonArray = (JSONArray) jsonObject.get(area.province);
- jsonArray.put(area.city);
- } else {
- JSONArray jsonArray = new JSONArray();
- jsonArray.put(area.city);
- jsonObject.put(area.province, jsonArray);
- }
- }
- System.out.println(jsonObject);
- }
-
- static class Area {
- private final String province;
- private final String city;
-
- Area(String province, String city) {
- this.province = province;
- this.city = city;
- }
- }
- }
运行结果:
{"上海":["浦东"],"四川":["成都","攀枝花"],"福建":["福州","厦门","泉州"]}
参考文献:
http://www.blogjava.net/woxingwosu/archive/2008/01/24/133301.html