如何预解析JSON字符串? [英] How do I pre parse a JSON String?

查看:90
本文介绍了如何预解析JSON字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将此JSON URL馈送到另一个站点

I want to feed this JSON URL to another site

http://naturetrek.co.uk/blog/api/get_recent_posts/

但是问题在于,由于前三个<p>标记无效,因此您可以在此处进行检查...

But the problem is that because of the initial three <p> tags its not valid JSON, you can check this in here...

http://jsonlint.com/

如果您删除了这3个标签,它将生效.

if u remove these 3 tags, it becomes valid.

我们无法更改Naturetrek博客输出的JSON字符串.

We cant change the JSON string output from the Naturetrek Blog.

在我们自己的代码中,我们使用以下内容....

In our own code we use the following....

<h2>Naturetrek BLOG</h2>
<div id="ntblog"></div>

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">

    $(document).ready(function () {

        var blogURL = "http://naturetrek.co.uk/blog/api/get_recent_posts/";

        $.getJSON(blogURL, function(info) {

            alert("here");
            var output = info.status;

            /*
            for (var i = 0; i <= info.posts.length - 1; i++) {
            output += '<li>' +
            '<a href = "' + info.posts[i].url +
            '">' + info.posts[i].title + '</a></li>';

            }
            */

            var ntblog = document.getElementById('ntblog');
            ntblog.innerHTML = output;
        });
    });
</script>

永远不会调用警报,因为JSON是无效的JSON.我的问题是-是否可以通过某种方式来准备JSON来删除三个<p>标记?还是您可以想到的另一种方法?

The alert never gets called, because the JSON is invalid JSON. MY QUESTION IS -- IS THERE A WAY TO PREPARSE THE JSON TO REMOVE THE THREE <p> TAGS, SOMEHOW? OR ANOTHER WAY U CAN THINK OF?

推荐答案

$.getJSON只是以下简称:

$.getJSON is just shorthand for:

$.ajax({
  dataType: "json",
  url: url,
  data: data,
  success: success
});

在此处查看文档: http://api.jquery.com/jQuery.getJSON/

只需使用$ .ajax将dataType更改为"html",然后使用$ .parseJSON解析成功函数中的修改后的JSON数据

Just use $.ajax change dataType to "html" and parse your modified JSON data in the success function with $.parseJSON

注意:由于您似乎正在执行跨域请求,因此必须使用jsonp,请在此处阅读更多信息:

Note: Since it seems you are doing a cross-domain request, you must use jsonp, read more here:

http://learn.jquery.com/ajax/working-with-jsonp /

这篇关于如何预解析JSON字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆