javascript - 将额外属性附加到 JSON 对象

标签 javascript json

我正在尝试将一个元素附加到我的 JSON 对象。 JSON结构如下:

    [
        {
            "newId": "167249-20",
            "title": "El Supremo ordena reabrir la causa por los delitos fiscales de Carlos Fabra",
            "created": "2011-12-01T13:22:00+01:00",
            "priority": "99999",
            "primaryCategoryId": "305",
            "summary": "La sala de lo penal de este órgano ha dejado sin efecto el archivo por prescripción de cuatro de los cinco delitos fiscales que se le imputan al expresidente de la Diputación de Castellón.",
            "image": "http%3A%2F%2Fwww.heraldo.es%2Fuploads%2Fimagenes%2Frec15%2F_reaperuradelcasofabra9558214_d07227b1.jpg",
            "timestamp": "1322742120"
        },
        {
            "newId": "167233-20",
            "title": "Victoria Rodríguez y Claire Morel se imponen en el Open de Andorra",
            "created": "2011-12-01T13:11:00+01:00",
            "priority": "5",
            "primaryCategoryId": "307",
            "summary": "Las patinadoras del Aramón Jaca y del CETDI, Victoria Rodríguez Long y Claire Morel se impusieron en sus respectivas categorías en el Open de Andorra de Patinaje sobre hielo disputado el pasado fin de semana en la pista de Canillo.",
            "image": "",
            "timestamp": "1322741460"
        }
]

所以我想添加额外的属性 (imageLocation),如下所示:

[
    {
        "newId": "167249-20",
        "title": "El Supremo ordena reabrir la causa por los delitos fiscales de Carlos Fabra",
        "created": "2011-12-01T13:22:00+01:00",
        "priority": "99999",
        "primaryCategoryId": "305",
        "summary": "La sala de lo penal de este órgano ha dejado sin efecto el archivo por prescripción de cuatro de los cinco delitos fiscales que se le imputan al expresidente de la Diputación de Castellón.",
        "image": "http%3A%2F%2Fwww.heraldo.es%2Fuploads%2Fimagenes%2Frec15%2F_reaperuradelcasofabra9558214_d07227b1.jpg",
        "timestamp": "1322742120",
        "imageLocation:" "xxx/xxx/xxx/xxx/xxx/xxx/jpg"
    },
    {
        "newId": "167233-20",
        "title": "Victoria Rodríguez y Claire Morel se imponen en el Open de Andorra",
        "created": "2011-12-01T13:11:00+01:00",
        "priority": "5",
        "primaryCategoryId": "307",
        "summary": "Las patinadoras del Aramón Jaca y del CETDI, Victoria Rodríguez Long y Claire Morel se impusieron en sus respectivas categorías en el Open de Andorra de Patinaje sobre hielo disputado el pasado fin de semana en la pista de Canillo.",
        "image": "",
        "timestamp": "1322741460",
        "imageLocation:" "xxx/xxx/xxx/xxx/xxx/xxx/jpg"
    }
]

最佳答案

如果您已将 JSON 字符串解析为名为 jsonObj 的 javascript 对象,则可以轻松循环遍历集合并添加新属性:

for (int i = 0; i<jsonObj.length;i++)
{
   var singleItem = jsonObj[0];
   singleItem.imageLocation = "http://someUrl/" + singleItem.newId; // example
}

如果您随后需要将 JSON 作为字符串,请使用 JSON.stringify(jsonObj)

如果您还没有将 JSON 字符串解析为 Javascript 对象,您可以轻松完成:

var jsonObj = JSON.parse(jsonString);

关于javascript - 将额外属性附加到 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8465886/

相关文章:

javascript - Angular 子组件无法识别输入更改

python - 在 Python 中序列化子集合(使用 jsonpickle)

json - 如何从 super JSON主体中拆分数据,以便获得具有多个键和值的 HashMap ?

javascript - 将 API 请求中的 JSON 数据插入 MySQL 数据库

javascript - Anuglar2 生命周期事件作为 rxjs Observable

javascript - Protractor 中的全局 beforeEach 和 afterEach

javascript - 正则表达式从文本中捕获 ID

javascript - 通过聚合名称或部分名称导入聚合

php - 使用 php 发送 json 帖子

java - 如何在java中使用map创建JSONArray