Collect four methods to share json parsing_javascript skills
Json is widely used in web development. As a carrier of data transmission, how to parse the data returned by Json is very common. Here are four ways to parse Json:
Part 1
var list1 = [1,3,4];
alert(list1[1]);
var list2 = [{"name":"leamiko","xing":"lin"}];
alert(list2[0]["xing"] )
alert(list2[0].xing)
Part 2
var value = {
"china":{
"hangzhou":{"item":"1"},
"shanghai":{"item":"2"},
"chengdu ":{"item":"3"}
},
"America":{
"aa":{"item":"1"},
"bb":{" item":"2"} 2"},
"ff":{"item":"3"}
}
};
for(var countryObj in value)
{
document.write( countryObj ":
")
//Useless for(var cityObj in value.countryObj)
for(var cityObj in value[countryObj])
{
document. write(' ' cityObj "
");
for(var itemObj in value[countryObj][cityObj])
{
document.write(" " itemObj value[countryObj][ cityObj][itemObj] "
")
}
} }
}
Explanation:
In short, it is crucial to distinguish whether it is json or array.
Part 3
Copy code
{"name":"shanghai", "item":"2"}, {"name":"sichuan", "item":"3"}
],
"America":[
{"name":"aa", "item":" 12"},
{"name":"bb", "item":"2"}
],
"Spain":[
{"name":"cc", " item":"1"},
{"name":"dd", "item":"23"},
{"name":"ee", "item":"3"}
]
};
for (var countryObj in value2)
{
document.write(countryObj ":
")
for (var cityObj in value2[countryObj])
{
//You can use document.write(" " value2[countryObj][cityObj].item "
");
document.write(cityObj " " value2[countryObj][cityObj]["name"] "
" );
}
}
Explanation:
countryObj is the attribute name of the value2 object, value2[countryObj] is the attribute value of the value2 object. In this example, it is an array, cityObj is an element of the array, and it is another json object, so, value2[countryObj] [cityObj]["name"] can access the attribute value of the object's name, or you can access the attribute value through value2[countryObj][cityObj].name.
Part 4
var value2 = {
"china":[
{"name":"hangzhou", "item":"1"},
"china":[
{"name":"shanghai", "item":"2 "},
{"name":"sichuan", "item":"3"}
],
"America":[
{"name":"aa", "item ":"12"},
", "item":"1"},
{"name":"dd", "item":"23"},
{"name":"ee", "item":"3 "}
]
};
for (var countryObj in value2)
{
document.write(countryObj ":
")
/ /document.write(" " value2[countryObj].length);
for (var i = 0;i < value2[countryObj].length; i )
{
document.write(" " value2[countryObj][i]["name"] "
");
}
}
The attribute name of the countryObj value2 object, value2[countryObj] attribute value. In this example, it is an array, value2[countryObj].length is the length of the array, and the items of the value2[countryObj][i] array == json object.
value2[countryObj][i]["name"] gets the value of name. You can also use value2[countryObj][i].name to get the value of name.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In the field of web development, XML and JSON, one of the data formats, are widely used, and the Gin framework is a lightweight Go language web framework that is simple, easy to use and has efficient performance. This article will introduce how to use the Gin framework to implement XML and JSON data parsing functions. Gin Framework Overview The Gin framework is a web framework based on the Go language, which can be used to build efficient and scalable web applications. The Gin framework is designed to be simple and easy to use. It provides a variety of middleware and plug-ins to make the development

Error handling in Golang: How to handle json parsing errors? In Golang, handling errors is a very important issue. Especially when dealing with external data, such as JSON data returned by network requests, we need to pay special attention to error handling. This article will introduce how to handle JSON parsing errors in Golang, and how to handle these errors gracefully. In Golang, JSON parsing errors are usually caused by incorrect data format or mismatched structure. when we look from the outside

Parsing JSON responses in Go: Use the Unmarshal function of the encoding/json package. Create a target structure that represents the JSON data. Read the HTTP response body and parse the JSON data. Print or use the parsed data.

With the development of the Internet era, data has become the basis of every form of information we are exposed to, and among them, JSON data format is often used in network data exchange. In order to facilitate the parsing and use of this data format, the Python language provides a JSON parsing library, which will be explained in detail in this article. 1. Introduction to JSON JSON (JavaScriptObjectNotation) is a lightweight data exchange format. Compared with XML, JSON is more concise and easier to

A JSONObject is an unordered collection of key-value pairs and parses a text string to produce a map-like object. A JSONObject has several important methods to display different types of values. For example, the getString() method is used to obtain the string associated with the key string, the getInt() method is used to obtain the integer value associated with the key, and the getDouble() method Used to get the double value associated with the key, the getBoolean() method is used to get the Boolean value associated with the key. Example importorg.json.*;publicclassJSONObjectTypeValuesTest{&

With the continuous development and application of Internet technology, front-end and back-end data interaction has become a necessary part of Web development. As a lightweight data exchange format, JSON has become one of the most popular choices. As an important language in the field of web development, PHP also provides a complete JSON processing function library, so developers can easily parse and generate JSON data. This article will introduce how to parse and generate JSON in PHP. 1. Basic concepts of JSON JSON (JavaScri

How to parse and generate JSON format in PHP In modern network development, JSON (JavaScriptObjectNotation) has become a commonly used data exchange format. It is lightweight, easy to read and write, and is widely used in various programming languages. PHP, as a popular server-side scripting language, also provides powerful support for parsing and generating JSON format data. This article will introduce how to parse and generate JSON format in PHP, including using

The Gson library can be used to parse JSON strings into tree models. We can use JsonParser to parse a JSON string into a tree model of type JsonElement. The getAsJsonObject() method of JsonElement can be used to obtain JsonObject and the getAsJsonArray() JsonElement method can be used to obtain elements in the form of JsonArray. Syntax publicJsonObjectgetAsJsonObject()publicJsonArraygetAsJsonArray()Example importjava.uti
