Key value pairs using JSON

后端 未结 4 2138
一向
一向 2020-12-23 22:41

Is there a way to handle data structures using JSON object in a way of Key/ Value pairs?
If so can some one elaborate how to access associated value object from the key

4条回答
  •  盖世英雄少女心
    2020-12-23 23:08

    JSON (= JavaScript Object Notation), is a lightweight and fast mechanism to convert Javascript objects into a string and vice versa.

    Since Javascripts objects consists of key/value pairs its very easy to use and access JSON that way.

    So if we have an object:

    var myObj = {
        foo:   'bar',
        base:  'ball',
        deep:  {
           java:  'script'
        }
    };
    

    We can convert that into a string by calling window.JSON.stringify(myObj); with the result of "{"foo":"bar","base":"ball","deep":{"java":"script"}}".

    The other way around, we would call window.JSON.parse("a json string like the above");.

    JSON.parse() returns a javascript object/array on success.

    alert(myObj.deep.java);  // 'script'
    

    window.JSON is not natively available in all browser. Some "older" browser need a little javascript plugin which offers the above mentioned functionality. Check http://www.json.org for further information.

提交回复
热议问题