Key value pairs using JSON

后端 未结 4 2137
一向
一向 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:28

    I see what you are trying to ask and I think this is the simplest answer to what you are looking for, given you might not know how many key pairs your are being sent.

    Simple Key Pair JSON structure

    var data = {
        'XXXXXX' : '100.0',
        'YYYYYYY' : '200.0',
        'ZZZZZZZ' : '500.0',
    }
    

    Usage JavaScript code to access the key pairs

    for (var key in data) 
      { if (!data.hasOwnProperty(key))
        { continue; } 
        console.log(key + ' -> ' +  data[key]);
      };
    

    Console output should look like this

    XXXXXX -> 100.0 
    YYYYYYY -> 200.0 
    ZZZZZZZ -> 500.0
    

    Here is a JSFiddle to show how it works.

提交回复
热议问题