0

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?

David
  • 48
  • 4
  • 3
    Can you elaborate on "cant load because the numbers are random."? – tholu Aug 20 '13 at 20:12
  • Do you mean you can't access the values because you don't know what the keys (timestamps) are? – gen_Eric Aug 20 '13 at 20:13
  • possible duplicate of [Converting .NET DateTime to JSON](http://stackoverflow.com/questions/1016847/converting-net-datetime-to-json) – OnoSendai Aug 20 '13 at 20:15
  • you can easily iterate then with something like `for (i in obj.message) { ... }` if you have to. – Marc B Aug 20 '13 at 20:16
  • i mean that i cant load the json file further because the timestamp is random. – David Aug 20 '13 at 20:16
  • 1
    @David the problem is that "can't load the JSON file" doesn't really mean anything. It's proper JSON, so it **is** possible to work with the string as JSON, and a JSON parser would be happy with it. – Pointy Aug 20 '13 at 20:17
  • I understand is must be cant load the json file further, i know that it is proper json. – David Aug 20 '13 at 20:22
  • 2
    @David: I don't think "load" is the word you are looking for. "Load" means to "download". I'm assuming that part works fine. The word you want is probably "traverse" or simply "read". – gen_Eric Aug 20 '13 at 20:25
  • 1
    @RocketHazmat Perhaps you might be looking for "parse". – Jeff Noel Aug 20 '13 at 20:31
  • @JeffNoel: That too. I assumed it was already parsed and he was just trying to get the data. – gen_Eric Aug 20 '13 at 20:41

3 Answers3

4

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);
}
bfavaretto
  • 71,580
  • 16
  • 111
  • 150
0

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.

Community
  • 1
  • 1
OnoSendai
  • 3,960
  • 2
  • 22
  • 46
  • The numbers aren't random but not the same! – David Aug 20 '13 at 20:22
  • 1
    I understand, but it seems to me that your code is using timestamps as indexes. As Pointy and David properly pointed out, this is a valid JSON content; the problem lies within your interpreter code. – OnoSendai Aug 20 '13 at 20:25
0

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);
Shane
  • 836
  • 6
  • 3