The question sounds weird I know. But its a weird question. Let me clarify.
Im using the facebook graph api. The feeds for my page are returned in JSON and i have this bit of JSON here:
"message": "A3Media Uk Website is fully up and running! Tell your friends, We can't make Beautiful Websites without clients!\n - Alex Morley-Finch",
"message_tags": {
"116": [
{
"id": "514033508",
"name": "Alex Morley-Finch",
"offset": 116,
"length": 17
}
]
}
So from my knowledge, there is an object called message_tags which contains and array of objects called 116, and index 0 of this array contains and object with the variables id, name, offset and length.
Now what i want to do is, obviously replace the text "Alex Morley-Finch" within the message variable with the tag "name": "Alex Morley-Finch". Then using the id, length and offset i can replace the text with a html link to that profile using the ID!
This all seem pretty simple, however, i obviously want my code to be dynamic so the code will work for ANY tag at ANY position.
The name of the object array "116" always matches the offset contained inside it.
The actual question:
How can I dynamically get the name of the object Array (in this case '116')?
Because my code would be something like (pseudo code):
if message has tag
get name of tag
if message contains name of tag
replace message name using offset and length with html link tag with href = facebook url / id
end if
end if
This would leave me with my html representation of the "message"
The thing is i cant get the name of the message_tag because id have to do somthing like:
// data[index] represents the current message
var json = JSON.parse(XmlHttpResponse.responseText);
json.data[index].message_tags.116[0].name;
as we can see this is not dynamic. This code will ONLY work for this tag.
So how do i get the name without referencing 116? is it even possible?
i was thinking about trying iterating through 1, 2, 3, 4, 5 --> 116 but that would be very performance costly AND bad coding AND im not even sure if you can reference arrays through variable names... ... ...
Im really stumped.
Please Help!
Alex
for...inloop. Side note: Your problem is actually not related to JSON but how to access properties of a JavaScript object.for...inloop is a link).message_tags.