-1

I have ajax call that produces this block of data

[
    {
        "2014-05-28": {
            "date": "28",
            "month": "05",
            "year": "2014",
            "data": [
                {
                    "title": "Html test <!-- comm -->  tesd ",
                    "link": "/tickets/ticket/3",
                    "progress": "33.33",
                    "creator": "Ivo Ivic",
                    "priority": "H"
                }
            ]
        },
        "2014-05-30": {
            "date": "30",
            "month": "05",
            "year": "2014",
            "data": [
                {
                    "title": "I ovo u petak",
                    "link": "/tickets/ticket/10",
                    "progress": "0.00",
                    "creator": "Ivo Ivic",
                    "priority": "H"
                },
                {
                    "title": "Do kraja tjedna!",
                    "link": "/tickets/ticket/9",
                    "progress": "0.00",
                    "creator": "Ivo Ivic",
                    "priority": "N"
                }
            ]
        }
    },
    "<div><h3>Expired deadlines</h3>\r\n    <div class=\"deadline_expired\" style=\"display: block;\">\r\n\t\t<span class=\"exp_deadline_title\">\r\n\t\t\t<div class=\"exp_deadline_priority_box\" style=\"background: #e82024\"></div>\r\n\t\t\t<a class=\"dashboard_titles\" href=\"/tickets/ticket/7\" title=\"Zadaća za novi projekt\">Zadaća za novi projekt</a>\r\n\t\t</span>\r\n\r\n\t\t<span class=\"deadline_info_wrapper\">\r\n\t\t\t<div class=\"exp_deadline_icon_box\"></div>\r\n\t\t\t<div class=\"deadline_date\">\r\n                <div class=\"deadline_text_color\">Deadline:</div>\r\n                <div class=\"deadline\">datum :(</div>\r\n            </div>\r\n\t\t\t<div class=\"deadline_no_days expired_days_style\">1 DAY AGO</div>\r\n\t\t</span>\r\n        <div class=\"clear\"></div>\r\n    </div>\r\n    <div class=\"deadline_expired\" style=\"display: block;\">\r\n\t\t<span class=\"exp_deadline_title\">\r\n\t\t\t<div class=\"exp_deadline_priority_box\" style=\"background: #f7cf2f\"></div>\r\n\t\t\t<a class=\"dashboard_titles\" href=\"/tickets/ticket/8\" title=\"Stari zadatak\">Stari zadatak</a>\r\n\t\t</span>\r\n\r\n\t\t<span class=\"deadline_info_wrapper\">\r\n\t\t\t<div class=\"exp_deadline_icon_box\"></div>\r\n\t\t\t<div class=\"deadline_date\">\r\n                <div class=\"deadline_text_color\">Deadline:</div>\r\n                <div class=\"deadline\">datum :(</div>\r\n            </div>\r\n\t\t\t<div class=\"deadline_no_days expired_days_style\">2 DAYS AGO</div>\r\n\t\t</span>\r\n        <div class=\"clear\"></div>\r\n    </div>\r\n</div>"
]

I am trying to get the 1st part (index 0) object to another object

Lets say the upper part i recived in a variable called DATA

So i am trying to get

var test= JSON.parse(DATA);
console.log('parsed: '+test[0]) // returns object Object

What i am trying to get is this Object {the data from 1st part}

Any clues on what i am doing wrong?

zinho
  • 225
  • 1
  • 2
  • 9
  • actually the link you sent as answer to this question doesn't help , as the question is similar but doesn't resolve my issue... to be more specific i have found other solution by taking the 0 index as one object and then passing it further.. but thanks for helping – zinho Jun 02 '14 at 09:06
  • Do not edit out your question content again, please. – Andrew Barber Sep 21 '14 at 21:13

2 Answers2

1

Simplifying your data from json... you have [{}]

Which is an array, containing an object.

When you access the [0], you get the object. You can see the json presentation with JSON.stringify(test[0])

Your object seems to have properties with keys defined as dates. If you want to access these, either you need to know the exact date and access it with myobject["2014-05-28"] or myobject.2014-05-28 (although I'm not certain this is valid with the -'s. Would be outright wrong if the key had spaces in it).

Alternatively you could iterate over the object properties.

for (var property in object) {
    if (object.hasOwnProperty(property)) {
        // object[property]
    }
}
Toni
  • 273
  • 3
  • 10
0

Are you trying to access inner data object? If yes then try this?

console.log(test[0]['2014-05-28'].data[0]);

and to get lets say title you would do

console.log(test[0]['2014-05-28'].data[0].title);
Matas Vaitkevicius
  • 58,075
  • 31
  • 238
  • 265