我有一些由字符串格式化的API返回的数据,我想将其转换为数组,以便我可以从中选择值。
如下所示:
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": []
}
}
因此,将前面的文本剪断,然后将其余部分传送到数组中。我怎么能用JS做到这一点?
答案 0 :(得分:1)
我认为你的意思是将字符串转换为json格式?如果我是对的,你可以尝试JSON
它可以将字符串转换为json,反之亦然。
alert(JSON.decode('[0,1,false,true,null,[2,3],{"some":"value"}]'))
// 0,1,false,true ,, 2,3,[object Object]
答案 1 :(得分:0)
您从API获得的响应是JSON响应。
说你有以下回应:
var response = {"data": "name", "Id" : "1"};
您可以使用response.data
或response.Id