Js parses json, js converts json to map, and obtains key, value, and jsonmap of map.

Source: Internet
Author: User

Js parses json, js converts json to map, and obtains key, value, and jsonmap of map.
Json string format

{   "10.10.11.1": {        "target_1": "34.2",        "target_3": "70.83",        "target_2": "50.51",        "target_5": "110.68",        "c_event": "event",        "target_4": "90.79",        "c_timestamp": "12:54:34",        "target_6": "10.2",        "c_subject": "10.18.11.0"    }}

{String, map <String, String >}similar to this format

Js functions:

function getValue(){var str = '{ "10.10.11.1": { "target_1": "34.2","target_3": "70.83","target_2": "50.51"}}';var map = eval("("+str+")");for (var key in map) {  var a = map[key];for (var k in a) {  alert(k+'---'+a[k]);}     }  }

Idea: parse the json string into a map object, but it is not supported in chrome.


When map is converted to json, the map key is converted to the json key.

You can use the syntax of JS for... in... to traverse all the keys and values in the object. The Code is as follows:
Var o = {"ret": {"2009051298180": [{"goods_price": 385, "goods_number": 1}], "2009051255518": [{"goods_price ": 960, "goods_number": 1}], "2009051268194": [{"goods_price": 0, "goods_number": 1}] }; var retObj = o. ret; for (var p in retObj) {// p is the key value you want alert (p + ':' + retObj [p]);}
 
Js json processing is the key-value mode. The key is dynamically obtained. The existing key-value Pair stuno, 001 json object is a, and I have obtained the key value.

Var str = '{"stuno": "0 "}';
Eval ('var json = '+ str );
// Var json = eval ('+ str + ')');
Alert (json. stuno); // The result is 0.
For (var name in json ){
Alert (name); // name result stuno
Alert (json [name]); // The result 0 is the same as the preceding result.
}
Or your requirements:
Var key = 'std ';
Alert (json [key]); // result 0 is the same as above
Yes ~

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.