网络编程 发布日期:2025/10/31 浏览次数:1
环境
概念
JSON 是一种按照JavaScript对象语法的数据格式,这是 Douglas Crockford 推广的。
虽然它是基于 JavaScript 语法,但它独立于 JavaScript,这也是为什么许多程序环境能够读取(解读)和生成 JSON。
JSON可以作为一个对象或者字符串存在,前者用于解读 JSON 中的数据,后者用于通过网络传输 JSON 数据。
JavaScript 提供一个全局的 可访问的 JSON 对象来对这两种数据进行转换。
JSON 结构
{
 "squadName" : "Super hero squad",
 "homeTown" : "Metro City",
 "formed" : 2016,
 "secretBase" : "Super tower",
 "active" : true,
 "members" : [
  {
   "name" : "Molecule Man",
   "age" : 29,
   "secretIdentity" : "Dan Jukes",
   "powers" : [
    "Radiation resistance",
    "Turning tiny",
    "Radiation blast"
   ]
  },
  {
   "name" : "Madame Uppercut",
   "age" : 39,
   "secretIdentity" : "Jane Wilson",
   "powers" : [
    "Million tonne punch",
    "Damage resistance",
    "Superhuman reflexes"
   ]
  },
  {
   "name" : "Eternal Flame",
   "age" : 1000000,
   "secretIdentity" : "Unknown",
   "powers" : [
    "Immortality",
    "Heat Immunity",
    "Inferno",
    "Teleportation",
    "Interdimensional travel"
   ]
  }
 ]
}
JSON 数组
[
 {
  "name" : "Molecule Man",
  "age" : 29,
  "secretIdentity" : "Dan Jukes",
  "powers" : [
   "Radiation resistance",
   "Turning tiny",
   "Radiation blast"
  ]
 },
 {
  "name" : "Madame Uppercut",
  "age" : 39,
  "secretIdentity" : "Jane Wilson",
  "powers" : [
   "Million tonne punch",
   "Damage resistance",
   "Superhuman reflexes"
  ]
 }
]
其他注意事项
对象和文本间的转换
可以使用 JSON.stringify 和 JSON.parse 来进行对象和文本间的转换。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。