将JSON转换为数组dataType

Convert JSON into array dataType

本文关键字:数组 dataType 转换 JSON      更新时间:2023-09-26

我有以下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);
}