How to retrieve data from json and data from json

Source: Internet
Author: User

How to retrieve data from json and data from json

First, json data is written as follows:

{"Head": [{"text": "Guangzhou", "id": "Guangzhou", "pid": "Guangdong Province" },{ "text ": "Zhengzhou", "id": "Zhengzhou", "pid": "Henan Province"}],}

If you wantExtract, You can only useLoopThe 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 wantSelective outputIf conditions must be added. 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)



How to extract data from the database and wrap it in json

* Convert List to json string
* @ ParamlistList
* @ ReturnStringjson string
Publicstatic <T> String getString4List (List <T> list)
JSONArray jsonArray = JSONArray. fromObject (list );
Return jsonArray. toString ();

How can I obtain the values in the following json data?

$. Each (date, function (I, n ){
Console. log (n. title );

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: 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.