当前位置:网站首页>JSON使用示例
JSON使用示例
2022-06-22 08:00:00 【Licht小粉】
1——什么是json?
JSON,说白了就是JavaScript用来处理数据的一种格式,这种格式非常简单易用。
JSON支持的语言非常多,包括JavaScript、C#、PHP、Java等等,这是由于JSON是独立于语言的轻量级的数据交换格式。
2——json的两种格式:
1.对象结构(键值对)无序
对象结构是使用大括号“{}”括起来的,大括号内是由0个或多个用英文逗号分隔的“关键字:值”对(key:value)构成的。
语法:
var jsonObj =
{
"键名1":值1,
"键名2":值2,
……
"键名n":值n
}jsonObj指的是json对象。
2.数组结构 有序
JSON数组结构是用中括号“[]”括起来,中括号内部由0个或多个以英文逗号“,”分隔的值列表组成。
语法:arr指的是json数组。在JSON数组中,每一对“{}”相当于一个JSON对象
var arr =
[
{
"键名1":值1,
"键名2":值2
},
{
"键名3":值3,
"键名4":值4
},
……
]注意,这里的键名是字符串,但是值可以是数值、字符串、对象、数组或逻辑true和false。
4——在 JavaScript 中使用 JSON示例
例如,可以创建一个新的 JavaScript 变量,然后将 JSON 格式的数据字符串直接赋值给它:
var people =
{ "programmers": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "[email protected]" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "[email protected]" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "[email protected]" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
]
}访问数据
实际上,只需用点号表示法来表示数组元素。所以,要想访问 programmers 列表的第一个条目的姓氏,只需在 JavaScript 中使用下面这样的代码:
people.programmers[0].lastName;注意,数组索引是从零开始的。
下面是使用同一变量的几个示例。
people.authors[1].genre // Value is "fantasy"
people.musicians[3].lastName // Undefined. This refers to the fourth entry, and there isn't one
people.programmers.[2].firstName // Value is "Elliotte"利用这样的语法,可以处理任何 JSON 格式的数据,而不需要使用任何额外的 JavaScript 工具包或 API。
修改 JSON 数据
正如可以用点号和括号访问数据,也可以按照同样的方式轻松地修改数据:
people.musicians[1].lastName = "Rachmaninov";在将字符串转换为 JavaScript 对象之后,就可以像这样修改变量中的数据。
转换回字符串
String newJSONtext = people.toJSONString();更重要的是,可以将任何 JavaScript 对象转换为 JSON 文本。并非只能处理原来用 JSON 字符串赋值的变量。为了对名为 myObject 的对象进行转换,只需执行相同形式的命令:
String myObjectInJSON = myObject.toJSONString();边栏推荐
- Do you want to modify the title of the website
- C语言双向链表实现图书管理系统 可读写文件
- itertools 排列组合
- Excellent cases of data visualization
- CollectionViewCell
- What is distributed transaction
- Vue page caching problem solving (keep alive + page level routing guard + lifecycle activated)
- What are uniapp, wap2app and 5 + app in dccloud?
- Blob format problems involved in image uploading
- Difference between ID instancetype nsobject *
猜你喜欢

丰田bZ4X取消上市发布会,就算低温充电问题不存在,产品力如何?

IP address planning

Xlua environment configuration

Note pad replaces all contents after a character in all lines

(7)双向链表

Node red sends wechat official account message (template message)

Energy and interference of waves

LR 2022 ultra detailed installation tutorial "latest"

【 Oracle database】 Nursery Mother Tutorial day13 date Function
![[songhongkang MySQL database] [advanced chapter] [07] MySQL storage engine](/img/47/62ad1e661e015438c40574f533d8f1.png)
[songhongkang MySQL database] [advanced chapter] [07] MySQL storage engine
随机推荐
C语言双向链表实现图书管理系统 可读写文件
FileTool
Win openfeign from simple to deep
MySQL index
什么是分布式事务
Windchill API drops
(9)顺序队列与栈队列
MySQL 8.0 under Linux forgets the password
Use js to download the current image
Realization of readable and writable files in library management system with C language bidirectional linked list
Simplicity is the best method of network promotion
Cocoapods problem record
先锋期货安全么?期货开户都是哪些流程?期货手续费怎么降低?
数据可视化优秀案例
KVO summary
[Oracle database] mammy tutorial day13 date function
Microsoft Remote Desktop 10.7.6正式版
MySQL view
Windchill - how to start workflow through API
IP address planning