[英]Convert String to Array with Javascript?
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. 我有一些以字符串格式设置的API返回的数据,我想将其转换为数组,以便从中选择值。
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?
如何使用JS做到这一点?
I think what you mean is convert the string to json format? 我认为您的意思是将字符串转换为json格式? If I am right you can try JSON
如果我是对的,您可以尝试JSON
It can convert string to json and vice-versa. 它可以将字符串转换为json,反之亦然。
alert(JSON.decode('[0,1,false,true,null,[2,3],{"some":"value"}]'))
// 0,1,false,true,,2,3,[object Object] // 0,1,false,true ,, 2,3,[object Object]
The response you have got from the API is JSON response. 您从API获得的响应是JSON响应。
Say you have the following response: 假设您有以下回应:
var response = {"data": "name", "Id" : "1"};
You can use response.data
or response.Id
您可以使用
response.data
或response.Id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.