json对象加json对象
We have heard time and again that everything in JavaScript is an object.
我们一次又一次地听到JavaScript中的所有东西都是对象。
This very fact was extensively used to weave the lightweight super popular data format called JSON. In JSON, all the data represented in key-value pairs goes inside a pair of opening and closing curly braces '{ }'. This indicates an object in JavaScript.
这个事实被广泛地用于编织轻量级超级流行的数据格式JSON。 在JSON中,键值对中表示的所有数据都位于一对大括号“ {}”内 。 这表示JavaScript中的对象。
JSON data can have objects inside it for example,
JSON数据中可以包含对象,例如,
{
"Name": "Yoshi",
"LastName":"Tatsu",
"Age": 24,
"Weapons": ["Sword","Shield","Ninja blade"]
}
The above represents a JSON object. Inside this object, we have key-value pairs of different data types for example name represents a string, age is a number and weapons being an array of strings. We can also have nested JSON objects, which means a key-value pair can be an object in itself containing multiple key-value pairs. For instance,
上面的内容代表一个JSON对象 。 在此对象内部,我们具有不同数据类型的键值对,例如,名称表示字符串,年龄是数字,武器是字符串数组。 我们还可以嵌套JSON对象 ,这意味着一个键值对可以是本身包含多个键值对的对象。 例如,
{
"Ninjas":[
"NinjaProperties":{
{
"Name": "Yoshi",
"LastName":"Tatsu",
"Age": 24,
"Weapons": ["Sword","Shield","Ninja blade"]
}
}
]
}
Inside our JSON object, we have a Ninjas array with another object NinjaProperties inside it which contains multiple key-value pairs. The NinjaProperties itself is a key and its value is a set of other values,
在JSON对象中 ,我们有一个Ninjas数组,其中包含另一个对象NinjaProperties ,其中包含多个键值对。 NinjaProperties本身是一个键,其值是一组其他值,
We can treat JSON object similar to the way we treat JavaScript objects,
我们可以像对待JavaScript对象一样对待JSON对象,
var pokemon={
"name":"Squirtles",
"type":"water",
"HP":100,
"isEvolved":false
}
Pokemon;
Output
输出量
{name: "Squirtles", type: "water", HP: 100, isEvolved: false}
We can access property names either using the dot notation or using the square bracket notation as we do in arrays. Let's see an example of each,
我们可以使用点表示法或方括号表示法来访问属性名称,就像在数组中一样。 让我们看一个例子,
pokemon.type;
Pokemon["name"];
Output
输出量
"water"
"Squirtles"
We can also assign new properties to the JSON object,
我们还可以为JSON对象分配新属性,
pokemon.info="pokedex";
Pokemon;
Output
输出量
{name: "Squirtles", type: "water", HP: 100, isEvolved: false, info: "pokedex"}
HP: 100
info: "pokedex"
isEvolved: false
name: "Squirtles"
type: "water"
__proto__: Object
Similarly, we can modify existing values too,
同样,我们也可以修改现有值,
pokemon.HP=40;
Pokemon;
Output
输出量
{name: "Squirtles", type: "water", HP: 40, isEvolved: false, info: "pokedex"}
You can see the HP of our Pokemon is now changed to 40 instead. Must be an arduous battle with another wild squirtle!.
您可以看到我们的神奇宝贝的生命值现在改为40 。 必须和另一个野生的松鼠进行艰苦的战斗!
We can also loop through our objects,
我们还可以遍历对象
for(t in pokemon){
console.log(t);
}
Output
输出量
name
type
HP
isEvolved
Info
Every element of the object is the key holding the values.
对象的每个元素都是保存值的键。
Let's how a real JSON object looks like, the kind of thing we receive from API's. Visit https://jsonplaceholder.typicode.com/users/2 on the browser to see a list of users in the form of some JSON data.
让我们看一下真正的JSON对象的样子,即我们从API收到的东西。 在浏览器上访问https://jsonplaceholder.typicode.com/users/2 ,以一些JSON数据的形式查看用户列表。
{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "[email protected]",
"address": {
"street": "Victor Plains",
"suite": "Suite 879",
"city": "Wisokyburgh",
"zipcode": "90566-7771",
"geo": {
"lat": "-43.9509",
"lng": "-34.4618"
}
},
"phone": "010-692-6593 x09125",
"website": "anastasia.net",
"company": {
"name": "Deckow-Crist",
"catchPhrase": "Proactive didactic contingency",
"bs": "synergize scalable supply-chains"
}
}
You can see we get the JSON data for user with id 2 and inside this JSON object we can also see two nested objects with the key of address and company.
您可以看到我们获得了ID为2的用户的JSON数据,并且在此JSON对象内,我们还可以看到两个嵌套的对象,其键为address和company 。
json对象加json对象