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 );
})