抓取instagram时parseJson的问题

时间:2014-07-25 20:44:23

标签: jquery json cors

我在尝试将json文本glob转换为我的cors应用程序中的json对象时遇到unrecognized expression错误的问题。看起来它有一个语法问题,但这很明显。有人能告诉我我错过了什么吗?

<script  type="text/javascript">
  $(function()
  {
    /**
     * Test URI: http://localhost?url=//instagram.com/p/gPFGUruPP4&app=callback_#
     */
    $.urlParam = function(name){
        var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
        return (null===results) ? null : results[1] || 0
    }

    if ($.urlParam('url') && $.urlParam('app')){
      var parts = {
        url: decodeURIComponent($.urlParam('url').replace('http://', '//')),
        app: "http://tool.throa.com/approve/" + $.urlParam('app')
      };

      $.ajaxPrefilter(function(options) {
        if(options.crossDomain && $.support.cors) {
          var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
          options.url = "http://cors.corsproxy.io/url=" + options.url;
        }
      });

      var cors = $.ajax({type: "GET", url: parts.url, async: false}).responseText;

      $(cors + ':contains("script")').each(function(){
           if($(this).children().length < 1 && $.trim($(this).text()).length != ''){
              var text = $(this).text();
              if (text.match(/^window._sharedData/) != null){
                var json = $.parseJson(text.replace('window._sharedData = ','').slice(0,-1));
                console.dir(json); // empty
              }
           } 
      });
    }
  });
</script>

根据请求完成json响应

{"entry_data":{"DesktopPPage":[{"canSeePrerelease":false,"viewer":null,"media":{"code":"gPFGUruPP4","date":1383445488.0,"usertags":{"nodes":[{"position":{"y":0.59074074,"x":0.6027778},"user":{"username":"circuit_theory"}}]},"comments":{"nodes":[{"text":"@circuit_theory I got the first one to try prototyping with, two more otw. We just need the copies now","viewer_can_delete":false,"id":"580714412939735991","user":{"username":"ehimeprefecture","profile_pic_url":"http:\/\/images.ak.instagram.com\/profiles\/profile_253293643_75sq_1352937431.jpg"}},{"text":"great! I am going to check out the yours widebody kit tgis week","viewer_can_delete":false,"id":"580944641012593138","user":{"username":"circuit_theory","profile_pic_url":"http:\/\/photos-e.ak.instagram.com\/hphotos-ak-xpf1\/10375745_240330439497996_286604756_a.jpg"}}]},"caption":"Toys from Japan #s30 #240z #nismo #works","likes":{"count":2,"viewer_has_liked":false,"nodes":[{"user":{"username":"dukes71","profile_pic_url":"http:\/\/images.ak.instagram.com\/profiles\/profile_179270872_75sq_1340040612.jpg"}},{"user":{"username":"mushakes14","profile_pic_url":"http:\/\/photos-b.ak.instagram.com\/hphotos-ak-xpa1\/925577_1444243859169113_1542103582_a.jpg"}}]},"owner":{"username":"ehimeprefecture","requested_by_viewer":false,"profile_pic_url":"http:\/\/images.ak.instagram.com\/profiles\/profile_253293643_75sq_1352937431.jpg","id":"253293643","followed_by_viewer":false},"is_video":false,"id":"580705301711877112","display_src":"http:\/\/photos-a.ak.instagram.com\/hphotos-ak-xap1\/1389505_219275871583688_142904055_n.jpg"},"__get_params":{},"staticRoot":"\/\/d36xtkk24g8jdx.cloudfront.net\/bluebar\/29e365f","__query_string":"?","prerelease":false,"__path":"\/p\/gPFGUruPP4\/","shortcode":"gPFGUruPP4"}]},"hostname":"instagram.com","config":{"csrf_token":"ec1bf60a3aa673f7e637e2cd317e85c8"},"static_root":"\/\/d36xtkk24g8jdx.cloudfront.net\/bluebar\/29e365f"}

1 个答案:

答案 0 :(得分:2)

JSON似乎很好。问题在于声明:

$(json).parseJson();

$使变量成为jQuery变量(jQuery“对象”)。但是,parseJSON()方法是一种“静态”方法,它不是“对象”方法(当然不是正确的术语,但更容易这样思考)。换句话说,它应该是:

$.parseJSON(json);

$jQuery或“类名”相同(错误的术语)。更不用说你忘记了大写字母^ _ ^。