对json的一些知识:
JSON(Javascript Object Notation)是一种轻量级的数据交换语言,以文字为基础,且易于让人阅读。尽管JSON是在Javascript的一个子集,但JSON是独立于语言的文本格式,并且采用了类似于C语言家族的一些习惯
。
首先要明白一点:{}表示一个对象,一个属性和值的关系是name:value
[]表示数组,并列的属性用‘,’
那么如何对这个东西读取呢?
<script type="text/javascript">
var a={ "programmers": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" }
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
] };
alert(a.programmers[0].email);//这个可以得到aaaa这个值。对像点出的是属性,数组后面可以跟着索引值。
alert(a.musicians[1].lastName);//这个可以得到Rachmaninoff"这个值
</script>
个人理解是:json就是跟java的对象相同。里面可以有各种属性。
并不是所有对象都是这么规整的,我们可以只要前面的原则读取就行。
<script type="text/javascript">
var a={ "programmers":“aa”,
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
],
"musicians":“cc“ };
alert(a.programmers);//这个可以得到aa这个值。