简体   繁体   English

使用:contains在dom中查找json,替换每个元素(单独)

[英]Find json in dom with :contains, replace each element (individually)

I have some server-side generated json string in a table, so I don't know what content might come up in it. 我在表中有一些服务器端生成的json字符串,所以我不知道其中可能包含什么内容。 I don't even know if there might be a JSON string or not. 我什至不知道是否可能有JSON字符串。 The table looks like this: 该表如下所示:

<table>
    <tr>
        <th> Some Header</th>
        <th> Some Header</th>
    </tr>
    <tr>
        <td> Something </td>
        <td>    
            {"Something":true,"Something":false,"Somthing":true,"Something":false,"Something":true,"Something else":false,"Something":false}
        </td>
    </tr>
    <tr>
        <td>Something</td>
        <td>
            {"Something":true,"Something":false,"Something":true,"Something":false,"Something":true,"Something else":false,"Something":false}
        </td>
    </tr>
</table>

I need to format each one of those json's strings into a formated table. 我需要将这些json字符串中的每个字符串格式化为格式化表。 I tried using :contains(":true"); 我尝试使用:contains(":true"); but the returning element looks like this: 但是返回的元素看起来像这样:

{"Something":true,"Something":false,"Something":true,"Something":false,"Something":true,"Something else":false,"Something":false}{"Something":true,"Something":false,"Something":true,"Something":false,"Something":true,"Something else":false,"Something":false} {“某物”:true,“某物”:false,“某物”:true,“某物”:false,“某物”:true,“其他物”:false,“某物”:false} {“某物”:true ,“ Something”:false,“ Something”:true,“ Something”:false,“ Something”:true,“ Otherothers”:false,“ Something”:false}

It returns the json's as expected, but I couldn't figure out a way to reference the containing element back to replace it with a formated table. 它按预期返回json,但是我想不出一种方法来引用包含元素以将其替换为格式化表格。 I did a different function for another table where every json string had a defined sibling, so I referenced that. 我为另一个表做了一个不同的功能,其中每个json字符串都有一个已定义的同级,所以我引用了它。 Here's that function: 这是该函数:

var jsonTable = function(s){
    var search = "th:contains("+s+")";
    var container = $(search).siblings();
    var string = container.text();
    var obj = jQuery.parseJSON(string);
    container.text(" ");
    var nospace = s.replace(/\s+/g, '');
    container.append("<table class='mini-table' id="+nospace+"></table>");
    jQuery.each(obj, function(text, val){
        $("#"+nospace).append("<tr><td class='large'>"+text+"</td><td class="+val+"></td></tr>");
    });
    return true;
};

So, my question is: 所以,我的问题是:

How can I target a specific element found using :contains when it returns several elements? 当返回多个元素时,如何定位使用:contains找到的特定元素? Any help or better way to do this is completely welcome. 完全欢迎您提供任何帮助或更好的方法。

Ps: Sorry if my english sucks, it isn't my native language. ps:对不起,如果我的英语不好,那不是我的母语。

You could do something like this. 你可以做这样的事情。 Code loops over each cell, checks if first character is { or [ and if it is, assumes contents are json. 代码遍历每个单元格,检查第一个字符是否为{[ ,如果是,则假定内容为json。 Could also check last character also and use a try/catch block as well to avoid JSON.parse errors being thrown 也可以检查最后一个字符并使用try/catch块,以避免抛出JSON.parse错误

$('td').each(function(){
    var txt=$.trim($(this).text());
    var reg=/\{|\[/;
    if(reg.test(txt.charAt(0)) ){
       /* text is probably json, should check last character also*/\

       /* would wrap this in try/catch for safety and additional validation that it is JSON*/
       var obj=JSON.parse( txt);

        /* "this" is td with json in it*/
        $(this).append('<span>This is json</span>')
    }  

})

DEMO 演示

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

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