Use a loop or if statement to retrieve data from json, ifjson

Source: Internet
Author: User

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.
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.