Jquery's method of parsing json strings and json arrays_jquery
The example in this article describes how Jquery parses json strings and json arrays. Share it with everyone for your reference. The details are as follows:
<!doctype html> <html> <head> <meta charset="utf-8"> <script src="js/jquery-1.6.2.min.js"></script> </head> <body> <hr /> <h3>解析json字符串、json数组</h3> <input type="button" id="jsonBtn" name="jsonBtn" value="jsonArray" /> <input type="button" id="jsonArray2" name="jsonArray2" value="jsonArray2" /> <input type="button" id="jsonStr" name="jsonStr" value="jsonStr" /> <input type="button" id="jsonStr2" name="jsonStr2" value="jsonStr2" /> <hr /> <div class="jsonText"> {"ret": 0, "msg": "", "is_lost":0, "nickname": "小米", "gender": "男", "province": "广东", "city": "广州", "year": "1990", "figureurl": "http:\/\/qzapp.qlogo.cn\/qzapp\/101152201\/D87BF108B19279F31587CE96E5648A98\/30", "figureurl_1": "http:\/\/qzapp.qlogo.cn\/qzapp\/101152201\/D87BF108B19279F31587CE96E5648A98\/50", "figureurl_2": "http:\/\/qzapp.qlogo.cn\/qzapp\/101152201\/D87BF108B19279F31587CE96E5648A98\/100", "figureurl_qq_1": "http:\/\/q.qlogo.cn\/qqapp\/101152201\/D87BF108B19279F31587CE96E5648A98\/40", "figureurl_qq_2": "http:\/\/q.qlogo.cn\/qqapp\/101152201\/D87BF108B19279F31587CE96E5648A98\/100", "is_yellow_vip": "0", "vip": "0", "yellow_vip_level": "0", "level": "0", "is_yellow_year_vip": "0"} </div> <hr /> <div class="jsonArray"> { root: [ {name:'1',value:'0'}, {name:'6101',value:'西安市'}, {name:'6102',value:'铜川市'}, {name:'6103',value:'宝鸡市'}, {name:'6104',value:'咸阳市'}, {name:'6105',value:'渭南市'}, {name:'6106',value:'延安市'}, {name:'6107',value:'汉中市'}, {name:'6108',value:'榆林市'}, {name:'6109',value:'安康市'}, {name:'6110',value:'商洛市'} ], json: [ {name:'6103',value:'宝鸡市'}, {name:'6104',value:'咸阳市'}, {name:'6107',value:'汉中市'}, {name:'6108',value:'榆林市'}, {name:'6110',value:'商洛市'} ] } </div> <hr /> <div class="jsonArray2"> [ {name:'1',value:'0'}, {name:'6101',value:'西安市'}, {name:'6102',value:'铜川市'}, {name:'6103',value:'宝鸡市'}, {name:'6104',value:'咸阳市'}, {name:'6105',value:'渭南市'}, {name:'6106',value:'延安市'}, {name:'6107',value:'汉中市'}, {name:'6108',value:'榆林市'}, {name:'6109',value:'安康市'}, {name:'6110',value:'商洛市'} ] </div> </body> </html> <script type="text/javascript"> ///jQuery 解析json字符串 //解析复杂的json数组 $("#jsonBtn").click(function(){ var data=$(".jsonArray").html(); alert("-----"+data); var dataObj=eval("("+data+")");//转换为json对象 alert(dataObj.root.length);//输出root的子对象数量 alert(dataObj.json.length);//输出json的子对象数量 //遍历json数组 $.each(dataObj.root, function(i, item) { alert(item.name+"-----root-------"+item.value); }); //遍历json数组 $.each(dataObj.json, function(i, item) { alert(item.name+"-----json-------"+item.value); }); }); //解析单个的json数组 $("#jsonArray2").click(function(){ var data=$(".jsonArray2").html(); alert("-----"+data); var dataObj=eval("("+data+")");//转换为json对象 alert(dataObj.length);//输出root的子对象数量 //遍历json数组 $.each(dataObj, function(i, item) { alert(item.name+"-----jsonArray-------"+item.value); }); }); ///解析标准的Json串 方法一 $("#jsonStr").click(function(){ var json=$(".jsonText").html(); alert("---2--"+json); var item = jQuery.parseJSON(json); alert(item.nickname); alert(item.ret); alert(item.figureurl ); }); ///解析标准的Json串,方法二 $("#jsonStr2").click(function(){ var json=$(".jsonText").html(); alert("---2--"+json); var obj = eval("("+json+")"); alert(obj.nickname); alert(obj.ret); alert(obj.figureurl ); }); </script>
I hope this article will be helpful to everyone’s jQuery programming.

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

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values takes a relatively long time.

Methods for deep copying arrays in PHP include: JSON encoding and decoding using json_decode and json_encode. Use array_map and clone to make deep copies of keys and values. Use serialize and unserialize for serialization and deserialization.

Multidimensional array sorting can be divided into single column sorting and nested sorting. Single column sorting can use the array_multisort() function to sort by columns; nested sorting requires a recursive function to traverse the array and sort it. Practical cases include sorting by product name and compound sorting by sales volume and price.

PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.

The best practice for performing an array deep copy in PHP is to use json_decode(json_encode($arr)) to convert the array to a JSON string and then convert it back to an array. Use unserialize(serialize($arr)) to serialize the array to a string and then deserialize it to a new array. Use the RecursiveIteratorIterator to recursively traverse multidimensional arrays.

PHP's array_group() function can be used to group an array by a specified key to find duplicate elements. This function works through the following steps: Use key_callback to specify the grouping key. Optionally use value_callback to determine grouping values. Count grouped elements and identify duplicates. Therefore, the array_group() function is very useful for finding and processing duplicate elements.
