Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
9
rated 0 times [  14] [ 5]  / answers: 1 / hits: 155986  / 14 Years ago, wed, september 15, 2010, 12:00:00

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



Assume that I have something like this



KEY1 |  VALUE OBJECT1 - (NAME: XXXXXX, VALUE:100.0)  
KEY2 | VALUE OBJECT2 - (NAME: YYYYYYY, VALUE:200.0)
KEY3 | VALUE OBJECT3 - (NAME: ZZZZZZZ, VALUE:500.0)

More From » jquery

 Answers
7

A JSON object is actually an oxymoron. JSON is a text format describing an object, not an actual object, so data can either be in the form of JSON, or deserialised into an object.



The JSON for that would look like this:



{KEY1:{NAME:XXXXXX,VALUE:100},KEY2:{NAME:YYYYYYY,VALUE:200},KEY3:{NAME:ZZZZZZZ,VALUE:500}}


Once you have parsed the JSON into a Javascript object (called data in the code below), you can for example access the object for KEY2 and it's properties like this:



var obj = data.KEY2;
alert(obj.NAME);
alert(obj.VALUE);


If you have the key as a string, you can use index notation:



var key = 'KEY3';
var obj = data[key];

[#95620] Sunday, September 12, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kalistaz

Total Points: 0
Total Questions: 100
Total Answers: 106

Location: Bermuda
Member since Thu, Apr 20, 2023
1 Year ago
;