Hi chaps, I wonder if you can help me with this.
I have the following script:

$(document).ready(function(){
        init_get_data();
});
var cars_data;
function init_get_data(){
    my_data={"data1":{"-xmlns:fo":"http://www.w3.org/1999/XSL/Format","-xmlns:inlineData":"Lookup","TheAttributes":{"-price":"Price","-other_attribute":"other_value"...

console.log(my_data.data1.TheAttributes.price);
}

Being very new to json I am trying to write on the firebug console to check how to get the values out of the json code. Unfortunately with the above the console returns undefined, and I was wondering why (after I manage to access one value I will try to loop through the json in order to get out more data). I also tried this console.log(my_data.data1.TheAttributes.price); but the console doesn't like it at all, it returns an error. I wonder if those hyphens before the data are a problem or not
thanks

Recommended Answers

All 4 Replies

Without the dash it works (Opera). With the dash you get an error on it. Because it is object notation, variable naming rules apply, and a dash is not a valid character in a name.

without dash it only works in opera though, firefox returns null. Point taken about the object notation. That json code (I only pasted a fraction of it, it is absolutely huge) is an XML extract. I have used this http://jsonviewer.stack.hu/ to convert from xml to json, so the dashes I presume have been inserted in the process. DO you think it is worth going through the json code and remove all the hyphens then? The thing is if it works only in opera I might not take the trouble to do it, I need that to work crossbrowser. DO you think that removing the dashes will sort everything out everywhere else? My test suggests this is not the case, because, as said, I have remove the hyphen and still no joy although it doesn't bring up an error.

I did change the order though, come to think of it. First the var, then the function, then the document ready.

Why do you pass all the XML as JSON ? Wouldn't it be easier to process more server-side.

ok, hyphens removed it all works now! thanks. there are issues with the server side...it is a long story but at the moment it seems the best way.
About about looping through it get out the data and say use it to replace what's on the html? What's the best way to loop through json objects?
However, this is part of a bigger thing, so I think after this I will open another thread, rather than change the subject in this, it makes more sense

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.