简体   繁体   English

JSON漂亮的印花与突出显示

[英]JSON pretty print with highlighting

I would like to pretty print JSON on a web page and highlight some text / lines in it. 我想在网页上打印JSON并突出显示其中的一些文本/行。

Ideally I am searching for a IFRAME - service to which I can link and URL where the JSON get's donwloaded and displayed as HTML, but I would like to specify an search string, which should be highlighted or the whole line containing the search string should be highlighted. 理想情况下,我正在搜索一个IFRAME - 我可以链接的服务和URL,其中JSON get被加载并显示为HTML,但我想指定一个搜索字符串,应突出显示或包含搜索字符串的整行应该是突出显示。 The JSON is public so there is no privacy issue. JSON是公开的,因此没有隐私问题。

If there is no such service, is there a Javscript library which supports highlighting? 如果没有这样的服务,是否有支持突出显示的Javscript库?

Focusing in more on your question about iframes - it's an issue in itself . 更多地关注关于iframe的问题 - 这本身就是一个问题 It's not possible to do what you want in an iframe if the domain names aren't the same. 如果域名不相同,则无法在iframe中执行您想要的操作。 However there are some workarounds for the same-origin policy that could help you in this situation. 但是,对于同源策略,有一些解决方法可以帮助您解决这种情况。


Ideally the service you're pulling from supports so you don't have to deal with iframes and can do what you like with the json response without worrying about the same-origin policy. 理想情况下,您所提供的服务支持因此您无需处理iframe,并且可以使用json响应执行您喜欢的操作,而无需担心同源策略。

As mentioned in a previous answer of mine you can use Prettify to apply syntax highlighting, though you can't highlight a specific line (from what I've found so far). 正如我之前的回答中所提到的,你可以使用Prettify来应用语法高亮,虽然你不能突出显示特定的行(从我到目前为止所发现的)。 For this example I'll be using the GitHub API . 对于这个例子,我将使用GitHub API

For the HTML, you would have: 对于HTML,您将拥有:

<pre id="jsonCode" class="prettyprint lang-json"></pre>

And the JavaScript to fetch and pretty print the JSON response (switch out if you'd like): 并且用于获取和漂亮打印JSON响应的JavaScript(如果您愿意,可以切换出 ):

$.getJSON('https://api.github.com/users/trevorsenior?callback=?', 
    function(data) {
        var jsonString = JSON.stringify(data, null, 4);
        $('#jsonCode').text(jsonString);
        prettyPrint(); //apply syntax highlighting to to JSON
    });

You can look at a working demonstration here: http://plnkr.co/edit/RiDtloVflmfuOrAokNXE?p=preview 您可以在此处查看工作演示: http//plnkr.co/edit/RiDtloVflmfuOrAokNXE?p = preview

If you do decide to use Prettify take a look at their getting started guide. 如果您决定使用Prettify,请查看他们的入门指南。


Update 更新

To fully answer your question, it is easy enough to add in highlighting to some text by wrapping the text in <span> tags with a specified class. 要完全回答您的问题,通过将文本包装在带有指定类的<span>标记中,可以很容易地为某些文本添加突出显示。 I've thrown together another example of this that builds off of the previous one: http://plnkr.co/edit/FM6Ua4pOvMW7nFFggdBy?p=preview 我把另一个基于前一个例子的例子扔到了一起: http//plnkr.co/edit/FM6Ua4pOvMW7nFFggdBy?p = preview

In a nutshell: 简而言之:

.highlighted {
  background-color: #ff0;
}
$('#search').keyup(function() {
  var search = $('#search').val();
  if(jsonString.match(search)) {
    var regex = new RegExp(search, 'g');
    var highlighted = '<span class="highlighted">' + search + '</span>';
    var newJsonString = jsonString.replace(regex, highlighted);
    $('#jsonCode').html(prettyPrintOne(newJsonString));
  } else {
    $('#jsonCode').html(prettyPrintOne(jsonString));
  }
});

If you want to remove the dynamic functionality & highlight on load simply move the logic out from the event listener: 如果要删除动态功能并在加载时突出显示,只需从事件侦听器中移出逻辑:

var highlight = function(jsonString, searchFor) {
    var regex = new RegExp(searchFor, 'g');
    var highlighted = '<span class="highlighted">' + searchFor + '</span>';
    var newJsonString = jsonString.replace(regex, highlighted);
    return prettyPrintOne(newJsonString);
};

And call it just before you populate the area with the code 在用代码填充区域之前调用它

$('#jsonCode').html(highlight(jsonString, 'X-RateLimit'));

Demonstration: http://plnkr.co/edit/be3SNq1TzeiPKXohXOk9?p=preview 演示: http//plnkr.co/edit/be3SNq1TzeiPKXohXOk9?p =preview

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

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