views:

66

answers:

2

I've got some Data being returned by an API that is formatted in a string, I want to convert it to an array so I can pick values out of it.

Comes in like the following:

images/20100819_202433.jpg{
"permalink": "http://burstn.com/sayhi#burst/06d67eed55d05dd545583de5b4ca3556", 
"total_comments": "0", 
"caption": "", 
"created_at": "2010-08-20 03:24:35.759172+0000", 
"coordinates": {
    "latitude": "", 
    "longitude": ""
}, 
"public": "False", 
"source": "wesbos_webcam", 
"total_likes": "0", 
"user": {
    "username": "sayhi", 
    "display_name": null, 
    "permalink": "http://burstn.com/sayhi", 
    "profile_image": "http://www.gravatar.com/avatar.php?default=http%3A%2F%2Fburstn.com%2Fimages%2Favatar.jpg&size=100&gravatar_id=e1cb97806d9df68d0e5daec9810cb228", 
    "id": 357
}, 
"image": {
    "large": "http://media.burstn.com/06d67eed55d05dd545583de5b4ca3556-large.jpg", 
    "width": 500, 
    "medium": "http://media.burstn.com/06d67eed55d05dd545583de5b4ca3556-medium.jpg", 
    "square": "http://media.burstn.com/06d67eed55d05dd545583de5b4ca3556-square.jpg", 
    "thumb": "http://media.burstn.com/06d67eed55d05dd545583de5b4ca3556-thumb.jpg", 
    "small": "http://media.burstn.com/06d67eed55d05dd545583de5b4ca3556-small.jpg", 
    "original": "http://media.burstn.com/06d67eed55d05dd545583de5b4ca3556-original.jpg", 
    "height": 500
}, 
"id": "06d67eed55d05dd545583de5b4ca3556", 
"comments": {
    "paging": {
        "previous": "", 
        "next": "http://burstn.com/api/1/comments/?burst_id=06d67eed55d05dd545583de5b4ca3556&page=2"
    }, 
    "data": []
}

}

So, snip the front text off and then convery the rest into an array. How can I do this with JS?

+1  A: 

I think what you mean is convert the string to json format? If I am right you can try JSON

It can convert string to json and vice-versa.

alert(JSON.decode('[0,1,false,true,null,[2,3],{"some":"value"}]'))

// 0,1,false,true,,2,3,[object Object]

Eugene Ramirez
Yes, javascript does not have named arrays, but you can use an object like one.
Okay, so how do I isolate a part of the above?
Wes
I would recommend the [json2.js](http://www.json.org/json2.js) library, because most modern browsers (even IE8) support the standard `JSON` object natively, and this library will use the native implementation if available. `JSON.parse` is the standard method to parse a JSON formatted string to a JavaScript object.
CMS
A: 

The response you have got from the API is json reponse.

say you have the following response. var response = {"data": "name", "Id" : "1"};

You can use response.data or reponse.Id
kapser