balaji
balaji

Reputation: 57

How to parse the JSON Date format from the below given json data using jquery?

{
"2013\/02\/05":[                        
{"id":"84eb13cfed01764d9c401219faa56d53","colour":"#000000","category":"custom"}
],
}

I have used the jquery code given below.I am trying to access the date '2013/02/05' and the array elements like id ,colour and category of that date.

     $(document).ready(function(){
      var output = $("#changeBtn");
      $("#data").click(function(){
        $.getJSON("json_data.json",function(jd){
               var dates = jd.date;
               alert(dates);
                });
          });

Upvotes: 1

Views: 74

Answers (1)

Kevin Bowersox
Kevin Bowersox

Reputation: 94429

The object returned is an associative array so you can access the property as follows:

$.getJSON("json_data.json",function(jd){
               var dates = jd["2013\/02\/05"][0].colour;
               alert(dates);
 });

JS Fiddle: http://jsfiddle.net/DLKfK/

On a side note, that is a pretty nasty object. I'm not sure why it needs to assign an array to the date property. If you have control over the object I would refactor it. One thing you should definitely refactor is the extra common after the array since this make the JSON invalid.

{
    "2013/02/05": [
        {
            "id": "84eb13cfed01764d9c401219faa56d53",
            "colour": "#000000",
            "category": "custom"
        }
    ], //this comma is invalid
} 

Upvotes: 1

Related Questions