David
David

Reputation: 48

JSON key as time

I've a json file in my ajax request, the ajax file is loaded, i know posible that my apologies for that.

i have a json file:

{
"message": {
    "1376819020": {
        "id": "151",
        "text": "Waar ? :)",
        "time": "1376819020",
        "fullname": "David"
    },
    "1376985491": {
        "id": "152",
        "text": "feff",
        "time": "1376985491",
        "fullname": "David"
    }
}

}

If i remove the time stamps ( 1376985491, 1376819020 ) i can load the json file into javascript but if i let the timestamps then i cant load because the numbers are random.

has any one a idea how i can load it?

Upvotes: 0

Views: 377

Answers (3)

Shane
Shane

Reputation: 836

If you are trying to access the data inside a specific object.

var json = {
  "message": {
    "1376819020": {
       "id": "151",
       "text": "Waar ? :)",
       "time": "1376819020",
       "fullname": "David Blokzijl"
    },
    "1376985491": {
       "id": "152",
       "text": "feff",
       "time": "1376985491",
       "fullname": "David Blokzijl"
    }
  }
};

Try accessing the property like ["1376985491"] instead of .1376985491

alert(json.message["1376985491"].id);

Upvotes: 0

bfavaretto
bfavaretto

Reputation: 71918

Use a for..in loop to iterate over the object keys:

var obj = {
    "message": {
        "1376819020": {},
        "1376985491": {}
    }
};

for(var timestamp in obj.message) {
    console.log(timestamp);
}

Upvotes: 4

OnoSendai
OnoSendai

Reputation: 3970

I do believe the numbers aren't random; They are milliseconds after epoch date.

You may find more information on how to convert them to valid javascript datetime here.

Upvotes: 0

Related Questions