当前位置: 首页 > 工具软件 > JSON::XS > 使用案例 >

json实例

孟凯泽
2023-12-01

官方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

 

 

 

  1. 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;  
  2.   
  3. import org.json.JSONArray;  
  4. import org.json.JSONObject;  
  5.   
  6. public class Test {  
  7.   
  8.     public static void main(String[] args) throws Exception {  
  9.         ArrayList<Area> areas = new ArrayList<Area>();  
  10.         areas.add(new Test.Area("福建""福州"));  
  11.         areas.add(new Test.Area("福建""厦门"));  
  12.         areas.add(new Test.Area("福建""泉州"));  
  13.         areas.add(new Test.Area("四川""成都"));  
  14.         areas.add(new Test.Area("四川""攀枝花"));  
  15.         areas.add(new Test.Area("上海""浦东"));  
  16.         JSONObject jsonObject = new JSONObject();  
  17.         for (Test.Area area : areas) {  
  18.             if (jsonObject.has(area.province)) {  
  19.                 JSONArray jsonArray = (JSONArray) jsonObject.get(area.province);  
  20.                 jsonArray.put(area.city);  
  21.             } else {  
  22.                 JSONArray jsonArray = new JSONArray();  
  23.                 jsonArray.put(area.city);  
  24.                 jsonObject.put(area.province, jsonArray);  
  25.             }  
  26.         }  
  27.         System.out.println(jsonObject);  
  28.     }  
  29.   
  30.     static class Area {  
  31.         private final String province;  
  32.         private final String city;  
  33.   
  34.         Area(String province, String city) {  
  35.             this.province = province;  
  36.             this.city = city;  
  37.         }  
  38.     }  
  39. }  

 

运行结果:

 

{"上海":["浦东"],"四川":["成都","攀枝花"],"福建":["福州","厦门","泉州"]}

参考文献:

http://www.blogjava.net/woxingwosu/archive/2008/01/24/133301.html

 类似资料: