json详解

来源:互联网 发布:dw格式化js代码 编辑:程序博客网 时间:2024/05/22 13:31
1 json 对象的访问 ,例如:
<script type="text/javascript">var s  = { "section":{"title":"Book-Signing Event","signing":[{"author":{"title":"Mr","name":"Oliver Sacks"},"book":{"title":"A Suitable Boy","prince":"$22.95"}},{"author":{"title":"Mr","name":"Oliver Sacks"},"book":{"title":"The Island of the Color-Blind","prince":"$12.95"}}]}};alert(s.section.title);alert(s.section.signing[0].author.title);alert(s.section.signing[1].book.title);</script>

2 通过特定格式的字符串生成json对象,例如:

String jsonContent = "{'hello':'world','abc':'xyz'}";JSONObject jsonObject = new JSONObject(jsonContent);String str1 = jsonObject.getString("hello");String str2 = jsonObject.getString("abc");System.out.println(str1);System.out.println(str2);

3 通过特定字符串生成json数组

jsonContent = "[{'hello':333, 'abc':false, 'xyz':'test'}, {'hello':555, 'abc':true, 'xyz':'test'}]";JSONArray jsonArray = new JSONArray(jsonContent);for(int i = 0; i < jsonArray.length();i++){JSONObject jsonObject2 = jsonArray.getJSONObject(i);int value1 = jsonObject2.getInt("hello");boolean value2 = jsonObject2.getBoolean("abc");String value3 = jsonObject2.getString("xyz");System.out.println(value1);System.out.println(value2);System.out.println(value3);}

4json的定义

var allOfficeList = {};

赋值 allOfficeList[officeList.options[i].value]=officeList.options[i].text;

遍历:for(var s in allOfficeList){
alert(s + allOfficeList[s]);
}


原创粉丝点击