0

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

Edited by Violet_82

2
Contributors
4
Replies
6
Views
5 Years
Discussion Span
Last Post by Violet_82
0

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.

0

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.

0

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.

Edited by pritaeas

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.