How to access JSON object in JavaScript

摘要: Below is a JSON string.

Below is a JSON string.

JSON String
    "name": "mkyong",
    "age": 30,
    "address": {
        "streetAddress": "88 8nd Street",
        "city": "New York"
    },
    "phoneNumber": [
            "type": "home",
            "number": "111 111-1111"
        },
            "type": "fax",
            "number": "222 222-2222"

To access the JSON object in JavaScript, parse it with JSON.parse(), and access it via “.” or “[]”.

JavaScript
<script>
       var data = '{"name": "mkyong","age": 30,"address": {"streetAddress": "88 8nd Street","city": "New York"},"phoneNumber": [{"type": "home","number": "111 111-1111"},{"type": "fax","number": "222 222-2222"}]}';
	var json = JSON.parse(data);
	alert(json["name"]); //mkyong
	alert(json.name); //mkyong
	alert(json.address.streetAddress); //88 8nd Street
	alert(json["address"].city); //New York
	alert(json.phoneNumber[0].number); //111 111-1111
	alert(json.phoneNumber[1].type); //fax
	alert(json.phoneNumber.number); //undefined
</script>	

Reference

  1. Wikipedia : JSON

上一篇: jQuery loop over JSON string $.each example
下一篇: Spring MVC find location using IP Address (jQuery + Google Map)
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章来源于互联网, 若有侵权, 联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布文章.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号