儿童嗓子疼吃什么药| 银消病用什么药效果最好| 收缩压是什么意思| 专科女生学什么专业好| 王羲之的儿子叫什么名字| 孩子生化了是什么意思| 为什么男生| 吃什么补叶酸最快| 骨性关节炎吃什么药| 手掌有痣代表什么| 落红的血是什么样子的| 轻微脑震荡有什么症状| 什么虫子有毒| 洗耳恭听什么意思| 耳朵烫是什么原因| 周围神经炎是什么症状| 血脂稠吃什么药| 宝宝咬人是什么原因| 氟比洛芬是什么药| 血少一撇念什么| ufo是什么意思| 皮肤擦伤用什么药最好| 丁香是什么| 什么是it行业| 尿痛吃什么药效果最好| 三月29号是什么星座| 返聘是什么意思| 为什么人不会飞| 白玉菩提是什么材质| 张飞的武器是什么| 薛之谦为什么离婚| 平方是什么意思| 桂花什么时候开| 麒麟到了北极会变成什么| 择期什么意思| 反哺是什么意思| 阔绰什么意思| 内痔疮用什么药治最好效果最快| 虾为什么叫对虾| 坐月子送什么礼物好| 做什么生意挣钱| 低回声结节什么意思| 梦见老公出轨什么意思| jeep是什么意思| 褥疮是什么| 什么孩子该看心理医生| 补气血喝什么| 生理曲度存在是什么意思| 杜甫被称为什么| 居住证签注是什么意思| 子宫内膜厚是什么意思| 幼儿急疹是什么原因引起的| 与狼共舞男装什么档次| 什么在千里| 喝酒手麻是什么原因| 诙谐幽默是什么意思| 气虚吃什么药| 可孚属于什么档次| 吃什么可以修复子宫内膜| 石英是什么| 胃酸胃胀反酸水吃什么药| 印第安人属于什么人种| 景色什么| 2003年属什么| 打开图片用什么软件| 菊花有什么功效和作用| 眼肿是什么原因引起的| 大运什么意思| 梦见男朋友出轨了是什么意思| 娃娃鱼吃什么食物| 聚乙二醇是什么东西| 玻璃的原材料是什么| 前列腺炎忌口什么食物| 滨海新区有什么好玩的地方| 金色配什么颜色好看| 口语化是什么意思| 吃什么凉血效果最好| 肿瘤指标偏高什么意思| 胃窦充血水肿意味着什么| 乳房痛是什么原因| 最好的避孕方法是什么| 夏天为什么这么热| 转学需要什么手续| 眼皮发肿是什么原因| 作怪是什么意思| 媛交是什么意思| b型血阳性是什么意思| 肩周炎有什么症状| 喉部有异物感是什么病| 我用什么留住你| 8月1日什么星座| 脚后跟痛什么原因| 后会有期什么意思| fox什么意思| sc1是什么意思| 人绒毛膜促性腺激素是什么意思| bobby什么意思| 激素六项检查挂什么科| 来月经腰疼是什么原因| 吃什么治便秘最有效| 什么是气胸有什么症状| 一直头疼是什么原因| 1990年什么命| 农历六月十七是什么星座| 稀释是什么意思| 睡不着挂什么科| 济公搓的泥丸叫什么| 怀疑心梗做什么检查| 东莞市委书记什么级别| honor是什么牌子| 王昆念什么| gr什么意思| 范是什么意思| 小孩老是肚子疼是什么原因| 人咬人有什么危害| 血脂稠吃什么药| 三丧日是什么意思| 小孩子流鼻血是什么原因| 备孕喝苏打水什么作用| 炖羊肉放什么| 俗气是什么意思| 什么叫眼睛散光| 奥运会五环颜色分别代表什么| 大便羊屎粒是什么原因| 厅堂是什么意思| 茶壶嘴为什么不能对着人| 99属什么生肖| 什么节气开始凉快| 降甘油三酯吃什么食物最好| 多吃什么对肾好| fr是什么| 女人的网名叫什么好听| 聿字五行属什么| hold on什么意思| 昙花什么时间开花| 三国是什么朝代| pra是什么意思| 下雨为什么会打雷闪电| louisvuitton什么牌子| 胃火吃什么食物好| 水命和什么命最配| 5月31日是什么星座| 高考是什么| 什么发型适合自己| 为什么肝区隐隐作痛| 宽慰是什么意思| 附睾炎是什么原因引起的| 脑ct挂什么科| 高考450分能上什么学校| 身体燥热是什么原因| 这个季节有什么水果| 粤菜是什么口味| 小狗拉肚子吃什么药| 打乙肝疫苗挂什么科| 性激素检查是查什么| 隔夜茶为什么不能喝| 月经期体重增加是什么原因| 主动权是什么意思| 对照是什么意思| 笨什么笨什么| 行是什么意思| 脚趾麻是什么病的前兆| 瘘是什么意思| 拉大便肛门口疼痛什么原因| 2005年是什么生肖| 漏斗胸是什么原因造成的| 木乐读什么| 手脚心发热是什么原因| 为什么16岁不能吃维生素B| 什么风什么月| 牛和什么生肖最配| 左耳朵嗡嗡响是什么原因引起的| 什么东西进去是硬的出来是软的| gap是什么意思| 什么书比较好| 被螨虫咬了用什么药膏| 冷淡是什么意思| 西梅什么时候上市| 四级警长是什么级别| 婴儿黄疸高有什么影响| 用鸡蛋滚脸有什么好处| 1990年1月属什么生肖| 姑姑的女儿叫什么| 医保什么时候到账| 3月12是什么星座| 一品诰命夫人是什么意思| 今天冬至吃什么| 经常不吃晚饭对身体有什么影响| 牵牛花是什么颜色| 角化型脚气用什么药膏| 饮食清淡主要吃什么| 4月份是什么星座| 清汤寡水是什么意思| 蟋蟀长什么样| omega3是什么意思| 梦见自己来月经了什么预兆| 梦见自己尿裤子了是什么意思| 女人梦见虫子什么预兆| 1月28号是什么星座| 移车打什么电话| 骨髓不造血是什么病| emba是什么| 00属什么| 隐血阳性是什么意思| 今年28岁属什么生肖| 什么是甲减有什么症状| 伤口好转的迹象是什么| 肺部条索影是什么意思| 李亚男为什么选王祖蓝| 独善其身是什么意思啊| 倾向是什么意思| 天鹅吃什么| 不领情是什么意思| 什么是滑精| 痛风是什么感觉| 肌酐为什么会升高| 乳腺增生的前兆是什么| 和田玉和翡翠有什么区别| ami是什么意思| 呕吐腹泻是什么原因| 打喷嚏流鼻涕吃什么药| 大米粉做什么好吃| 明天是什么| 司长是什么级别的官| 叶公好龙是什么生肖| 双马尾是什么意思| 嘴上长痘痘是什么原因| hco3-是什么意思| 梦见小白蛇是什么预兆| 章子怡是什么脸型| 黄喉是什么| 脸色发黑是什么病的前兆| hibor是什么意思| 火车硬卧代硬座是什么意思| 搞破鞋什么意思| 哔哩哔哩会员有什么用| 7月17日什么星座| 口蘑不能和什么一起吃| 臭鳜鱼是什么菜系| 家奴是什么生肖| 治疗胃反酸烧心用什么药最好| 有点咳嗽吃什么药| 婚煞是什么意思| 什么黄| 什么补肾最好| 物竞天择是什么意思| 开字五行属什么| 姑息性化疗什么意思| 三文鱼有什么营养| 拍ct挂什么科| 什么是工科| 强的松是什么药| 角质增生是什么意思| 血压计什么牌子好| 乳房检查挂什么科| 白葡萄酒配什么食物| 羊肚菌是什么| 尔尔是什么意思| 老年人吃什么钙片补钙好| 飞机什么不能带| 上市公司是什么意思| 死不瞑目是什么意思| 列席人员什么意思| 嘛呢是什么意思| 百度
JAVASCRIPT BASIC
JAVASCRIPT & DOM
JAVASCRIPT & BOM
JAVASCRIPT ADVANCED
JAVASCRIPT EXAMPLES
JAVASCRIPT REFERENCE

美国一大学生跑半马每英里喝一瓶啤酒惊呆众人

百度   如何提高睡眠质量?  养成良好的作息习惯。

In this tutorial you will learn how to encode and decode JSON data in JavaScript.

What is JSON

JSON stands for JavaScript Object Notation. JSON is extremely lightweight data-interchange format for data exchange between server and client which is quick and easy to parse and generate.

Like XML, JSON is also a text-based format that's easy to write and easy to understand for both humans and computers, but unlike XML, JSON data structures occupy less bandwidth than their XML versions. JSON is based on two basic structures:

  • Object: This is defined as an unordered collection of key/value pairs (i.e. key:value). Each object begins with a left curly bracket { and ends with a right curly bracket }. Multiple key/value pairs are separated by a comma ,.
  • Array: This is defined as an ordered list of values. An array begins with a left bracket [ and ends with a right bracket ]. Values are separated by a comma ,.

In JSON, property names or keys are always strings, while the value can be a string, number, true or false, null or even an object or an array. Strings must be enclosed in double quotes " and can contain escape characters such as \n, \t and \. A JSON object may look like this:

{
    "book": {
        "name": "Harry Potter and the Goblet of Fire",
        "author": "J. K. Rowling",
        "year": 2000,
        "genre": "Fantasy Fiction",
        "bestseller": true
    }
}

Whereas an example of JSON array would look something like this:

{
    "fruits": [
        "Apple",
        "Banana",
        "Strawberry",
        "Mango"
    ]
}

Tip: A data-interchange format is a text format which is used to interchange or exchange data between different platforms and operating systems. JSON is the most popular and lightweight data-interchange format for web applications.


Parsing JSON Data in JavaScript

In JavaScript, you can easily parse JSON data received from the web server using the JSON.parse() method. This method parses a JSON string and constructs the JavaScript value or object described by the string. If the given string is not valid JSON, you will get a syntax error.

Let's suppose we've received the following JSON-encoded string from a web server:

{"name": "Peter", "age": 22, "country": "United States"}

Now, we can simply use the JavaScript JSON.parse() method to convert this JSON string into a JavaScript object and access individual values using the dot notation (.), like this:

// Store JSON data in a JS variable
let json = '{"name": "Peter", "age": 22, "country": "United States"}';

// Converting JSON-encoded string to JS object
let obj = JSON.parse(json);

// Accessing individual value from JS object
alert(obj.name); // Outputs: Peter
alert(obj.age); // Outputs: 22
alert(obj.country); // Outputs: United States

Please check out the tutorial on PHP JSON parsing to learn, how to return JSON data from a web server in response, as well as, how to encode/decode JSON data on server side using PHP.


Parsing Nested JSON Data in JavaScript

JSON objects and arrays can also be nested. A JSON object can arbitrarily contains other JSON objects, arrays, nested arrays, arrays of JSON objects, and so on. The following example will show you how to parse a nested JSON object and extract all the values in JavaScript.

/* Storing multi-line JSON string in a JS variable
using the new ES6 template literals */
let json = `{
    "book": {
        "name": "Harry Potter and the Goblet of Fire",
        "author": "J. K. Rowling",
        "year": 2000,
        "characters": ["Harry Potter", "Hermione Granger", "Ron Weasley"],
        "genre": "Fantasy Fiction",
        "price": {
            "paperback": "$10.40", "hardcover": "$20.32", "kindle": "$4.11"
        }
    }
}`;

// Converting JSON object to JS object
let obj = JSON.parse(json);

// Define recursive function to print nested values
function printValues(obj) {
    for(let k in obj) {
        if(obj[k] instanceof Object) {
            printValues(obj[k]);
        } else {
            document.write(obj[k] + "<br>");
        };
    }
};

// Printing all the values from the resulting object
printValues(obj);

document.write("<hr>");

// Printing a single value
document.write(obj["book"]["author"] + "<br>");  // Prints: J. K. Rowling
document.write(obj["book"]["characters"][0] + "<br>");  // Prints: Harry Potter
document.write(obj["book"]["price"]["hardcover"]);  // Prints: $20.32

Encoding Data as JSON in JavaScript

Sometimes JavaScript object or value from your code need to be transferred to the server during an Ajax communication. JavaScript provides JSON.stringify() method for this purpose which converts a JavaScript value to a JSON string, as shown below:

Stringify a JavaScript Object

The following example will show you how to convert a JavaScript object to JSON string:

// Sample JS object
let obj = {"name": "Peter", "age": 22, "country": "United States"};

// Converting JS object to JSON string
let json = JSON.stringify(obj);
alert(json);

The output of the above example will look something like this:

{"name":"Peter","age":22,"country":"United States"}

Note: Although, JavaScript and JSON objects look quite similar, but they are not exactly the same. For example, in JavaScript, object property names can be enclosed in single quotes ('...') or double quotes ("..."), or you can omit the quotes altogether. But, in JSON, all property names must be enclosed in double quotes.

Stringify a JavaScript Array

Similarly, you can convert the JavaScript arrays to JSON strings, like this:

// Sample JS array
let arr = ["Apple", "Banana", "Mango", "Orange", "Papaya"];

// Converting JS array to JSON string
let json = JSON.stringify(arr);
alert(json);

The output of the above example will look something like this:

["Apple","Banana","Mango","Orange","Papaya"]

Warning: Do not use eval() function to evaluate JSON data (e.g., including function definitions in JSON string and converting them back into executable functions with eval() method), as it allows attacker to inject malicious JavaScript code into your application.

Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties
honor是什么牌子的手机 bally属于什么档次 透析是什么原理 闰6月是什么意思 什么地发现
送女生什么礼物比较好 圆谎是什么意思 郁郁寡欢是什么意思 灰溜溜是什么意思 mido手表什么档次
做梦梦到蛇是什么征兆 牛肉炖什么好吃 芥末黄是什么颜色 无穷是什么意思 血常规主要检查什么
猪油用什么容器装好 7月去青海带什么衣服 老天爷叫什么名字 手足口病咳嗽吃什么药 luxury什么牌子
肚子胀不消化吃什么药hcv9jop6ns1r.cn 博士点是什么意思hcv8jop5ns5r.cn 瘢痕子宫什么意思hcv9jop3ns0r.cn 十一月二十五是什么星座hcv8jop3ns1r.cn 胯疼是什么原因hcv8jop7ns7r.cn
花开半夏什么意思hcv7jop7ns0r.cn 眼睛肿是什么问题hcv7jop5ns4r.cn 背痛是什么原因hcv9jop2ns5r.cn 跟腱是什么hcv7jop6ns7r.cn 肝藏血是什么意思hcv8jop0ns3r.cn
酒后大量出虚汗什么原因baiqunet.com 巧克力是什么材料做的hcv9jop1ns2r.cn 白细胞计数偏高是什么意思hcv8jop2ns6r.cn 什么奶茶最贵hcv7jop7ns2r.cn 好是什么意思hcv8jop9ns8r.cn
jc是什么牌子hcv8jop3ns9r.cn 心脏早搏是什么原因hcv9jop4ns5r.cn 七月份适合种什么蔬菜hcv7jop6ns2r.cn 上发条是什么意思chuanglingweilai.com 看扁桃体挂什么科hcv8jop5ns7r.cn
百度