json对象加json对象_JSON对象

news/2024/7/19 13:36:16 标签: json, java, python, javascript, js

json>json对象加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://json>jsonplaceholder.typicode.com/users/2 on the browser to see a list of users in the form of some JSON data.

让我们看一下真正的JSON对象的样子,即我们从API收到的东西。 在浏览器上访问https://json>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对象内,我们还可以看到两个嵌套的对象,其键为addresscompany

翻译自: https://www.includehelp.com/json>json/json>json-objects.aspx

json>json对象加json>json对象


http://www.niftyadmin.cn/n/1253769.html

相关文章

Codeforces 1110C (思维+数论)

题面 传送门 分析 这种数据范围比较大的题最好的方法是先暴力打表找规律 通过打表,可以发现规律如下: 定义\(x2^{log_2a1}\) (注意,cf官方题解这里写错了,官方题解中定义\(x2^{log_2a}\)是有问题的 (1) 若\(a \neq 2^x…

CF1110D Jongmah

题意 给出\(n\)个在\(1\)到\(m\)范围内的数\(a_i\),定义一个三元组为\((x,x,x)\)或者\((x,x1,x2)\)的形式,求最多能组成三元组的数量是多少 题解 比赛的时候脑抽了……也可能是过年之后就傻了…… 先看看贪心行不行,把所有能选\(3\)个的都先选…

SpringMVC整合Junit4进行单元测试

使用步骤: 1.在src/test/java下建立一个测试包2.创建一个测试基类3.把要测试的类拷到该包下,并继承基类注意:编写测试类的原则: ①测试方法上必须使用Test进行修饰②测试方法必须使用public void 进行修饰,不能带任何的参数③新建一个源代码…

java awt画布_Java AWT画布

java awt画布Canvas is a GUI component that creates a rectangular box on the screen. It can be used to draw shapes or print text, it acts as a canvas. It can also be used to take user inputs. It is called so because it is like a canvas on which artists draw…

Scala中的HashSet

Scala HashSet (Scala HashSet) A HashSet is a special type of collection that is unordered i.e. the order of inserted elements is not defined. For storing its elements and keeping track of them it uses hashmaps. HashSet是一种无序的特殊集合类型,即…

设计模式(十)——模板方法模式

AbstractClass(抽象类):在抽象类中定义了一系列基本操作(PrimitiveOperations),这些基本操作可以是具体的,也可以是抽象的, 每一个基本操作对应算法的一个步骤,在其子类中可以重定义或实现这些步…

ruby 生成哈希值_Ruby中的哈希

ruby 生成哈希值Ruby Hash集合 (Ruby Hash Collection) In Ruby, hashes are the collection of identical keys and their values. They are like dictionaries, which has an element along with its description. You can retrieve any available value with the help of a …

.NET Core开发日志——Dapper与MySQL

.NET Core开发日志——Dapper与MySQL 原文:.NET Core开发日志——Dapper与MySQLDapper作为.NET生态中广为人知的轻量级ORM类库在.NET Core里仍能被有效利用,并且其不但可以连通SQL Server数据库还提供对其它数据库,比如MySQL的支持。这里试验了一下通过D…