码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript如何处理JSON数据

时间:2015-05-29 11:27:12      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

JSON (Javescript Object Notation)一种简单的数据格式,比xml更轻巧。 JSON 是 JavaScript 原生格式,这意味着在 JavaScript 中处理 JSON 数据不需要任何特殊的 API 或工具包。

JSON的规则很简单: 对象是一个无序的“‘名称/值’对”集合。一个对象以“{”(左括号)开始,“}”(右括号)结束。每个“名称”后跟一个“:”(冒号);“‘名称/值’ 对”之间使用“,”(逗号)分隔

举个简单的例子:

js 代码

function showJSON() {    
    var user =    
    {    
    "username":"andy",    
    "age":26,    
    "info": { "tel": "13564254568", "cellphone": "6526975"},    
    "address":    
    [    
    {"city":"shanghai","postcode":"111222"},    
    {"city":"newyork","postcode":"333444"}    
    ]    
    }    
    
    alert(user.username);    
    alert(user.age);    
    alert(user.info.cellphone);    
    alert(user.address[0].city);    
    alert(user.address[0].postcode);    
    } 

这表示一个user对象,拥有username, age, info, address 等属性。

同样也可以用JSON来简单的修改数据,修改上面的例子

js 代码

function showJSON() {    
    var user =    
    {    
 "username":"andy",    
    "age":26,    
    "info": { "tel": "13564254568", "cellphone": "6526975"},    
    "address":    
    [    
    {"city":"shanghai","postcode":"111222"},    
    {"city":"newyork","postcode":"333444"}    
    ]    
}    
alert(user.username);    
alert(user.age);    
alert(user.info.cellphone);    
alert(user.address[0].city);    
alert(user.address[0].postcode);    
user.username = "Tom";    
alert(user.username);     }  

JavaScript如何处理JSON数据

标签:

原文地址:http://www.cnblogs.com/lijshui/p/4537879.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!