Json noobie question - different selection methods

Hi perps,

I think I have a fairly simple json question, I’m a bit of a json noob.
What are the different methods of selecting some of the values in the json object below:
<script type=“text/javascript”>
var jsondata = [{ “data” : { “http://thissite.com/” : { “Attributes” : null,
“SubRows” : { “07/12/2011” : { “Attributes” : null,
“SubRows” : null,
“measures” : { “Views” : 24.0,
“Visits” : 12.0
}}}}}
}]
//what different selecting methods can you use to get the date, or the views?
//could you give me a document.writeln example please?

</script>

Thanks in advance :slight_smile:


var jsondata = [{
    "data": {
        "http://thissite.com/": {
            "Attributes": null,
            "SubRows": {
                "07/12/2011": {
                    "Attributes": null,
                    "SubRows": null,
                    "measures": {
                        "Views": 24.0,
                        "Visits": 12.0
                    }
                }
            }
        }
    }
}];

var data = jsondata[0].data;
for (var site in data) {
    if (data.hasOwnProperty(site)) {
        console.log("site: %s",site);
        console.log(data[site].Attributes);
        for (var date in data[site].SubRows) {
           if (data[site].SubRows.hasOwnProperty(date)) {
               console.log("date: %s",date);
               console.log("views: %s",data[site].SubRows[date].measures.Views);
               console.log("visits: %s",data[site].SubRows[date].measures.Visits);
           }
        }
    }
}

I’ve used console.log to output the values stored in your JSON. You just need to check to see when you are iterating through your JSON that the value exists before you use it - using .hasOwnProperty() is a good way to do this, for the sake of keeping the code brief I haven’t done that in the example above.

Thanks Anthony, awesome reply!

I’m trying to work with a library and pass the json values into another function. However the function only asks for the names or positions of the values (sorry my terminology might suck).
The example they give is:


window.store = new Ext.data.JsonStore({
  fields: ['name', 'data1', 'data2', 'data3', 'data4', 'data5'],
    data: [
        {'name':'Monday', 'data1':10, 'data2':12, 'data3':14, 'data4':8, 'data5':13},
        {'name':'Tuesday', 'data1':7, 'data2':8, 'data3':16, 'data4':10, 'data5':3},
        {'name':'Wednesday', 'data1':5, 'data2':2, 'data3':14, 'data4':12, 'data5':7},
        {'name':'Thursday', 'data1':2, 'data2':14, 'data3':6, 'data4':1, 'data5':23},
        {'name':'Friday', 'data1':27, 'data2':38, 'data3':36, 'data4':13, 'data5':33}
    ]
});

I want to use the following json in place of the example json above, but am not sure how to reference it in the “fields” parameter. (There will be more json data, but it follows this format).


data :[{
    "data": {
        "http://thissite.com/": {
            "Attributes": null,
            "SubRows": {
                "07/12/2011": {
                    "Attributes": null,
                    "SubRows": null,
                    "measures": {
                        "Views": 24.0,
                        "Visits": 12.0
                    }
                }
            }
        }
    }
}]

I hope this makes enough sense to follow. Let me know if you need more info.

Thanks for your help! :slight_smile: