繁体   English   中英

如何修改一个json对象?

[英]How to modify a json object?

是否可以从img标签中获取src 我有一个对象,我需要删除图像标记值示例

输入是这个

[

    {
        "N": "ABC corp",
        "a": "<img src=\"/img/samples/flag_red.gif\" alt=\"red\" height=\"16\" width=\"16\" border=\"0\"/>",
        "c": "<img src=\"/img/samples/flag_red.gif\" alt=\"red\" height=\"16\" width=\"16\" border=\"0\"/>",
        "r": "<img src=\"/img/samples/flag_green.gif\" alt=\"green\" height=\"16\" width=\"16\" border=\"0\"/>",
        "p": "<img src=\"https://a.na7.visual.force.com/resource/1260007793000/a/iconset/gray.gif\" alt=\" \" height=\"16\" width=\"1\" border=\"0\"/>"

    }
]

我想要的输出是这个

[
    {
        "N": "3M",
        "a": "red",
        "c": "red",
        "r": "red",
        "p": "gray"

    },

    {
        "N": "ABC corp",
        "a": "red",
        "c": "red",
        "r": "green",
        "p": "gray"

    }
]

如果我想要的属性值在img标签的src属性中,我如何从给定的输入对象创建这个新对象? 我需要检查输入对象属性中img标签的src属性。 如果是flag_red.gif则输出对象中的值应该是red 如果是flag_green.gif则输出对象中的值应该是green 如果它是gray.gif那么输出对象中的值应该是gray

下面向您展示如何根据图像标签的alt属性修改您的数据,让您大致了解。 只需很少的努力,您就可以轻松地将其修改为使用src属性进行修改。

var data = [{
    "N": "3M",
        "a": "<img src=\"/img/samples/flag_red.gif\" alt=\"red\" height=\"16\" width=\"16\" border=\"0\"/>",
        "c": "<img src=\"/img/samples/flag_red.gif\" alt=\"red\" height=\"16\" width=\"16\" border=\"0\"/>",
        "r": "<img src=\"/img/samples/flag_red.gif\" alt=\"red\" height=\"16\" width=\"16\" border=\"0\"/>",
        "p": "<img src=\"https://a.na7.visual.force.com/resource/1260007793000/a/iconset/gray.gif\" alt=\" \" height=\"16\" width=\"1\" border=\"0\"/>"

}, {
    "N": "ABC corp",
        "a": "<img src=\"/img/samples/flag_red.gif\" alt=\"red\" height=\"16\" width=\"16\" border=\"0\"/>",
        "c": "<img src=\"/img/samples/flag_red.gif\" alt=\"red\" height=\"16\" width=\"16\" border=\"0\"/>",
        "r": "<img src=\"/img/samples/flag_green.gif\" alt=\"green\" height=\"16\" width=\"16\" border=\"0\"/>",
        "p": "<img src=\"https://a.na7.visual.force.com/resource/1260007793000/a/iconset/gray.gif\" alt=\" \" height=\"16\" width=\"1\" border=\"0\"/>"

}];

var modified = data.reduce(function (outArr, entry) {
    var outObj = {};
    for (key in entry) {
        if (entry.hasOwnProperty(key)) {
            var item = entry[key];
            var node = document.createElement("div");
            node.innerHTML = item;
            var img = node.querySelector("img");
            outObj[key] = img ? img.alt : item;
        }
    }
    outArr.push(outObj);
    return outArr;
}, []); //[{"N":"3M","a":"red","c":"red","r":"red","p":" "},{"N":"ABC corp","a":"red","c":"red","r":"green","p":" "}]

您只需要检查<img>标签,如果匹配,则使用substring获取文件名并将其设置回JSON

工作Plunker

var obj=[
{
    "N": "3M",
    "a": "<img src=\"/img/samples/flag_red.gif\" alt=\"red\" height=\"16\" width=\"16\" border=\"0\"/>",
    "c": "<img src=\"/img/samples/flag_red.gif\" alt=\"red\" height=\"16\" width=\"16\" border=\"0\"/>",
    "r": "<img src=\"/img/samples/flag_red.gif\" alt=\"red\" height=\"16\" width=\"16\" border=\"0\"/>"
},
{
    "N": "ABC corp",
    "a": "<img src=\"/img/samples/flag_red.gif\" alt=\"red\" height=\"16\" width=\"16\" border=\"0\"/>",
    "c": "<img src=\"/img/samples/flag_red.gif\" alt=\"red\" height=\"16\" width=\"16\" border=\"0\"/>",
    "r": "<img src=\"/img/samples/flag_green.gif\" alt=\"green\" height=\"16\" width=\"16\" border=\"0\"/>"
}
];

for(var k in obj){
    for (name in obj[k]) {
        if(obj[k][name].indexOf("<img") != -1) {
            // I have made logic based on "flag_", this can be change as per your requirement.
            var src= obj[k][name].substring(obj[k][name].indexOf("flag_")+5,obj[k][name].indexOf(".gif"));
            obj[k][name]=src;
        }
    }
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM