惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
博客园 - 三生石上(FineUI控件)
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MongoDB | Blog
MongoDB | Blog
小众软件
小众软件
Y
Y Combinator Blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
D
Docker
罗磊的独立博客
Microsoft Security Blog
Microsoft Security Blog
D
DataBreaches.Net
B
Blog
Vercel News
Vercel News
Recent Announcements
Recent Announcements
GbyAI
GbyAI
阮一峰的网络日志
阮一峰的网络日志
T
The Blog of Author Tim Ferriss
H
Hackread – Cybersecurity News, Data Breaches, AI and More
P
Proofpoint News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
宝玉的分享
宝玉的分享

博客园 - chromecrown

MacBook M3 安卓系统层开发环境搭建 C# 语言规范 spring 排除指定的类或者包扫描 java web spring 发送邮件 spring-data-redis和jedis版本对应收集总结 python 多线程和多进程基本写法 python 调用nmap javascript定时函数 UML学习备忘 听王竑锜老师讲课笔记 WebLogic 8.1 部署问题记录 struts 2 result-types 备忘 小小菜鸟学习mysql 基本操作命令 备忘 VMware虚拟机网络配置相关备忘 visual Studio C# 环境下 创建windows 服务 使用C#调用系统API实现锁定计算机 心情状态---所困 jsf 阶段性总结
JavaScript中JSONObject和JSONArray相关知识备忘(网络转载)
chromecrown · 2014-03-19 · via 博客园 - chromecrown

1.json的格式,有两种:

{"key": "value"} //JSONObject(对象)

[{"key1": "value1"}, {"key2": "value2"}] //JSONArray(数组)

2.json的遍历

假如json字符串如下:

{

"feature":"fresh_today",

"photos":[

{

"id":40581634,

"name":"Dandelion 5"

},

{

"id":40581608,

"name":"Dandelion 3"

}

]

}
可以看出来,最外面是个json对象,photos节点是个数组,遍历代码如下:

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

JSONObject jsonObject = JSONObject.fromObject(result);

String feature = jsonObject.getString("feature");

JSONArray photoArray = jsonObject.getJSONArray("photos");

for (int i = 0; i < photoArray.size(); i++) {

JSONObject object = (JSONObject) photoArray.get(i);

//或者用如下代码获取JSONObject

//JSONObject object = JSONObject.fromObject(photoArray.get(i));

int id = object.getInt("id");

String name = object.getString("name");

}

附上一个很好用的在线json格式化和校验的网站:http://jsonformatter.curiousconcept.com/