从字符串AJAX响应中获取JSON数组

时间:2014-09-10 13:12:38

标签: jquery ajax json

我从我的ajax请求中得到了这个回复。 Ajax调用函数php get_contents(),这是reqeusted页面的HTML输出。

<script type="text/javascript">

    var jsLoaded = '';

    var searchCategories = [{"title":"something","id":"3","count":28117},{"title":"something","id":"35","count":14647},{"title":"something","id":"1","count":1124},{"title":"something","id":"6","count":836},{"title":"something","id":"5","count":18645},{"title":"something","id":"7","count":4791},{"title":"something","id":"8","count":21117},{"title":"something","id":"76","count":179},{"title":"something","id":"9","count":81435},{"title":"something","id":"13","count":14695},{"title":"something","id":"10","count":1517},{"title":"something","id":"11","count":2544},{"title":"something","id":"14","count":826},{"title":"something","id":"74","count":128},{"title":"something","id":"12","count":835},{"title":"something","id":"79","count":785},{"title":"something","id":"57","count":1165},{"title":"something","id":"15","count":2160},{"title":"something","id":"16","count":5649},{"title":"something","id":"72","count":2521},{"title":"something","id":"17","count":518},{"title":"something","id":"55","count":5535},{"title":"something","id":"18","count":6712},{"title":"something","id":"19","count":710},{"title":"something","id":"32","count":363},{"title":"something","id":"80","count":1102},{"title":"something","id":"20","count":1585},{"title":"something","id":"21","count":16502},{"title":"something","id":"36","count":221},{"title":"something","id":"101","count":65},{"title":"something","id":"25","count":5644},{"title":"something","id":"111","count":208},{"title":"something","id":"26","count":1019},{"title":"something","id":"27","count":9061},{"title":"something","id":"29","count":8211},{"title":"something","id":"78","count":874},{"title":"something","id":"22","count":11956},{"title":"something","id":"28","count":2873},{"title":"something","id":"2","count":3223},{"title":"something","id":"24","count":3723},{"title":"something","id":"41","count":3989},{"title":"something","id":"53","count":1131},{"title":"something","id":"30","count":26333},{"title":"something","id":"31","count":4672},{"title":"something","id":"42","count":313},{"title":"something","id":"67","count":2182},{"title":"something","id":"75","count":164},{"title":"something","id":"83","count":1822},{"title":"something","id":"59","count":3242},{"title":"something","id":"69","count":994},{"title":"something","id":"33","count":2786},{"title":"something","id":"37","count":20823},{"title":"something","id":"65","count":5742},{"title":"something","id":"23","count":6346},{"title":"something","id":"81","count":711},{"title":"something","id":"43","count":1399},{"title":"something","id":"61","count":8359}];        var durationDetails = [{"count":25237,"name":"short"},{"count":75812,"name":"medium"},{"count":27213,"name":"long"},{"count":128262,"name":"all"}];

</script>

和其他一些HTML一起使用。我只需要解析JSON字符串。我试过:contains和.find()但没有成功。你们恳请我告诉我该怎么办?

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

在你的ajax成功回调中添加

success: function (r) {
    $(r).filter('script').appendTo('head');
    gotTheStuff();
}

然后你可以简单地使用变量

function gotTheStuff(){
    console.log(searchCategories);
    // do stuff with the array
}

http://jsfiddle.net/j5y6umbL/2/


可选地

success: function (r) {
    $.globalEval($(r).filter('script').text());
    gotTheStuff();
}