最近做广告组件的开发,用到了json格式,今天做了几个demo,就放这了
首先说说json格式的基本规则:
1)并列的数据之间用逗号(“,”)分隔
2)映射用冒号(“:”)表示
3)并列数据的集合(数组)用方括号(“[]”)表示
4)映射的集合(对象)用大括号(“{}”)表示
我的理解就是 中括号可套多个大括号,大括号客套多个中括号,逗号做分隔,冒号做映射
demo如下:
var json_array = ["test1","test2"]; //alert(json_array[0]); //json基本格式 var json_array_a={"city":"beijing","street":"dongcheng","postcode":"123456"}; //alert(json_array_a.city); //json嵌套格式 var json_array_b={"name":"wangjinyang","address":{"city":"beijing","street":"dongcheng","postcode":"123456"}}; alert(json_array_b.name+" 所在城市:"+json_array_b.address.city); //json数组格式 var json_array_c= [ {"name":"wangjinyang01","address":{"city":"beijing","street":"dongcheng","postcode":"123456"}}, {"name":"wangjinyang02","address":{"city":"beijing","street":"xicheng","postcode":"123456"}}, ] alert("数组长度:"+json_array_c.length+"\r\n 第一位仁兄:"+json_array_c[0].name+" 所在城市:"+json_array_c[0].address.city);