将JSON转换为数组dataType

时间:2014-08-25 04:12:51

标签: javascript jquery json

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

3 个答案:

答案 0 :(得分:0)

如果你想要它在数组中,那么你可以使用像下面的分割函数

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(","));
}

答案 1 :(得分:0)

试试这个:

$.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
});

<强> DEMO

答案 2 :(得分:0)

你可以试试这个:

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);
}