0

I am converting JSON data into a list structure, and I want to save the "AAA.BBB[0].CCC.DDD[5].EEE" format as the id so when a user modifies the content of that list item it modifies the JSON data associated with that location.

For Example AAA.BBB[0].CCC.DDD[5].EEE = 123

123 is a list item but I want the id to be saved as "AAA.BBB[0].CCC.DDD[5].EEE"

Would there be a better way to save the location in the id?

*Edit:

Sample Code:

JSON DATA: {"AAA":{"AAB":"Value1","AAC":"Value2","AAD":1,"AAE":"Value3","AAF":"{"ABC": "Value4"}}}

Soo the id for list item "Value4" would be AAA.AAF.ABC

function nodeIT(obj,output){    
for (var x in obj){
    if(!(obj[x] instanceof Object)){

        //var str =JSON.stringify(obj);   // Where im stuck!
        output +="<li id='"+str+x +"'>";
        output += (x + "=" + obj[x] + "<br />");
        output +="</li>";
    }
    else if((obj[x] instanceof Object)){

        var obj1 = obj[x];
        output+="<li>" +x + "<ul>";

        output=nodeIT(obj1,output);
    }

}
output += "</ul></li>";
return output;

}

1 Answers1

0

Instead of using the ID attribute and being forced to use a single string, you could take advantage of jQuery's .data() method, which lets you associate javascript objects with html elements.

So you do something like this when you're building the list elements:

var liElement = $('<li />')
    .text(x + "=" + obj[x])
    .data({
        container: obj,
        key: x
    });

And then access the data later like this (where this refers to an li):

var container = $(this).data('container');
var key = $(this).data('key');
var value = container[key];
// .. modify value
container[key] = value;

Please see a full example here: http://jsfiddle.net/h38Ec/

tcovo
  • 7,550
  • 2
  • 20
  • 13
  • Thanks. The only problem that I run into with that is, the JSON data I listed is just an example, the actual JSON data I am using in my code is really extensive, to the point where there can be duplicate values. The reason I am trying to go with location is because thats a hard location for where the data is stored in reference to the actual JSON data and there cannot be multiple in one location. So I didn't specify. – judomonkeykyle Mar 29 '13 at 18:56
  • @judomonkeykyle I don't understand the problem you're describing. Can you give an example of some data that causes a problem with this method, but works with an absolute location method? – tcovo Mar 29 '13 at 19:41