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

推荐订阅源

IT之家
IT之家
T
Tailwind CSS Blog
V
V2EX
阮一峰的网络日志
阮一峰的网络日志
H
Help Net Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
腾讯CDC
GbyAI
GbyAI
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Last Week in AI
Last Week in AI
A
About on SuperTechFans
L
LangChain Blog
Engineering at Meta
Engineering at Meta
F
Fortinet All Blogs
G
Google Developers Blog
The Cloudflare Blog
云风的 BLOG
云风的 BLOG
D
Docker
博客园 - 聂微东
博客园 - 司徒正美
Recent Announcements
Recent Announcements
MyScale Blog
MyScale Blog
U
Unit 42

博客园 - 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/