将JSON转换为数组dataType
Convert JSON into array dataType
我有以下JSON字符串
var json = {"result":[{"address":" Ardenham Court, Oxford Road ,AYLESBURY, BUCKINGHAMSHIRE ,UNITED KINGDOM","picture":"1.jpg","uniqueid":"8b54275a60088547d473d462763b4738","story":"I love my home. I feel safe, I am comfortable and I am loved. A home can't be a home without our parents and our loved ones. But sad to say, some are experiencing that eventhough their loved ones are in their houses, they are not loving each other. There is a big war. You can't call it a home."}]}
我想分别得到地址,图片,故事为了完成这个。我尝试了最近的答案在stackoverflow,但我不能实现它。
下面是我尝试过的,
$.each(json.result.address, function (index, value) {
// Get the items
var items = this.address; // Here 'this' points to a 'group' in 'groups'
// Iterate through items.
$.each(items, function () {
console.log(this.text); // Here 'this' points to an 'item' in 'items'
});
});
试试这个:
$.each(json.result, function (index, value) {
console.log(this.address); // this will give you all addresses
console.log(this.picture); //this will give you all pictures
console.log(this.uniqueid); //this will give you all unique id's
console.log(this.story); //this will give you all stories
});
如果你想要它在数组中,那么你可以使用下面的分割函数
var address=json.result[0].address.split(",");
var picture=json.result[0].picture.split(",");
var story =json.result[0].story.split(",");
如果json中有更多的1,则
var address=[];
var picture=[];
var story =[];
for (var i=0;i<json.result.length;i++){
address=address.concat(json.result[i].address.split(","));
picture=picture.concat(json.result[i].picture.split(","));
story =story .concat(json.result[i].story.split(","));
}
你可以试试:
for(x in json.result){
alert(json.result[x].address);
alert(json.result[x].picture);
alert(json.result[x].uniqueid);
alert(json.result[x].story);
}
相关文章:
- 如何在映射数组中添加换行符
- javascript结合了数组和字典
- 需要帮助设置json数组
- 不能从angular2中的子组件指定父组件中的数组
- 使用JS将数组转换为json对象
- 数组在递归方法中设置为null
- knockoutjs可观察数组
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 将数组从PHP传递到Javascript
- JavaScript数组排序(函数)用于对表行进行排序,而不是排序
- 在函数中添加数组元素的数值
- 无法通过数组映射绑定
- javascript中的数组出错
- 如何使用 node.js 比较两个 json 数组
- Javascript(Angular)从一个对象数组到第二个数组查找值
- 根据id将json数组组合为一个json数组
- 如何通过数组更新角度子范围
- 有没有一种方法可以添加相同的项目val=“0”;4〃;到JavaScript中数组的每个对象
- 在Javascript中转换对象数组
- Javascript,有没有一种方法可以将数组写成没有逗号或空格的单个文本字符串