如何使用jQuery修改JSON内容

时间:2014-06-12 10:35:32

标签: javascript jquery json

我正在使用某些JSON中的jQuery构建一个下拉列表,我正在获取该项目的完整数据并将其添加到我稍后在POST请求中使用的data-obj中。我需要在数据中插入一些信息,但无法知道如何操作。

我从服务器收到的数据如下所示:

{
        "genreId": 1,
        "genre": "Horror",
        "genreGroup": "Fiction",
        "Publishers": [{
                "publisher": "Random House",
                "books": [{
                        "book": "The Shining",
                        "releaseyears": [2012, 2013, 2014]
                }]
        }, {
                "publisher": "Penguin",
                "books": [{
                        "book": "Dracula",
                        "releaseyears": [2010]
                }, {
                        "book": "Taking Lives",
                        "releaseyears": [2013, 2014]
                }]
        }]
}

我需要添加派生字段' Original Genre'进入数据所以它看起来像:

{
        "genreId": 1,
        "genre": "Horror",
        "genreGroup": "Fiction",
        "Publishers": [{
                "publisher": "Random House",
                "originalgenre": "Horror",
                "books": [{
                        "book": "The Shining",
                        "releaseyears": [2012, 2013, 2014]
                }]
        }, {
                "publisher": "Penguin",
                "originalgenre": "Horror",
                "books": [{
                        "book": "Dracula",
                        "releaseyears": [2010]
                }, {
                        "book": "Taking Lives",
                        "releaseyears": [2013, 2014]
                }]
        }]
}

我认为相关的代码是:

   $(info.genre).each(function (i) {
             var bookitem = $('<a/>', {
                     text: this.genre,
                     'data-value': this.genre,
                     'class': 'selectable',
                     'data-name': "G" + (this.genre),
                     'data-obj': JSON.stringify(this),
                     'data-target': 'booksection'
             });
             listGenres.append($("<li/>").append(bookitem));
[....do other things ...]

所以当我添加data-obj元素时,我得到了可用的数据(就像它在第一个代码块中一样,我需要在构建列表之前修改它以添加额外的字段

我是如何实现这一目标的?

3 个答案:

答案 0 :(得分:3)

$.each(data.Publishers, function(i, v) {
    data.Publishers[i]['originalgenre'] = data.genre;
});

JS FIDDLE DEMO

答案 1 :(得分:2)

for (var i = 0; i < myJson.Publishers.length; i++) {
    myJson.Publishers[i].originalgenre = myJson.genre;
}

哪里

var myJson = {
        "genreId": 1,
        "genre": "Horror",
        "genreGroup": "Fiction",
        "Publishers": [{
                "publisher": "Random House",
                "books": [{
                        "book": "The Shining",
                        "releaseyears": [2012, 2013, 2014]
                }]
        }, {
                "publisher": "Penguin",
                "books": [{
                        "book": "Dracula",
                        "releaseyears": [2010]
                }, {
                        "book": "Taking Lives",
                        "releaseyears": [2013, 2014]
                }]
        }]
};

答案 2 :(得分:2)

我认为Publishers数组中的“Original genre”应该匹配主对象中的“genre”?如果是这种情况,应该这样做:

$.each(yourObject.Publishers, function(index, publisher) {
    publisher.originalgenre = yourObject.genre;
});