Use a loop or if statement to retrieve data from json, ifjson
First, json data is written as follows:
{"Head": [{"text": "Guangzhou", "id": "Guangzhou", "pid": "Guangdong Province" },{ "text ": "Zhengzhou", "id": "Zhengzhou", "pid": "Henan Province"}],}
As shown above, if you want to extract the id and pid data in sequence, you can only use loops. The Code is as follows:
Var head_id = ""; var head_pid = ""; for (var I = 0; I <data. head. length; I ++) {head_id + = data. head [I]. id + ""; // cyclically output json data head_pid + = data. head [I]. pid + "" ;}$ ("# city "). append ("city:" + head_id); $ ("# city "). append ("province:" + head_pid );
In this way, data in json is output after data.
If you want selective output, you need to add the if condition. The Code is as follows:
For (var I = 0; I <data. head. length; I ++) {if (data. head [I]. pid = "Henan Province") {// select the output json data head_pid + = data. head [I]. pid ;}}
Note that if the object contains multiple groups of data, data is used. head. the id is undefined because it does not indicate the group of data, such as data. head [0]. id. If the object contains only one group of data, you can directly use data. head. id output.
In addition, if the json data is garbled in Chinese, on the one hand, the jquery code called by json is viewed, and on the other hand, it may be a problem of compiling json data files.
The above is a little learning result of self-learning json. record it.
(Note: use multiple data in an object directly)
Can I use only the if statement to implement a loop in C language? How to Use the if statement to implement a loop? The following program is used as an example.
# Include <stdio. h>
Int main (){
Int I = 1, s = I;
Re:
If (I <= 5)
{
S = s * I;
I ++;
Goto re;
}
Printf ("% d \ n", s );
Return 0;
}
How to write the JSON if statement
First, correct a concept. JSON (JavaScript Object Notation) is a lightweight data exchange format. If statement does not exist. You should write JavaScript syntax. You can use data directly in else. For example, data = [{Name: Li}, {Age: 20},...] data. name to access "Li", through data. and then output the data to the page.