简体   繁体   English

jQuery检查JSON数据

[英]jquery check json data

I am using the google news search api and get back data in json format. 我正在使用Google新闻搜索api并以json格式获取数据。 I am using ajax to get the data and parse through it. 我正在使用ajax获取数据并通过它进行解析。

This is my code: 这是我的代码:

function doAjax(query){
    alert(query);
    var target = "https://ajax.googleapis.com/ajax/services/search/news?v=1.0&q="+query+"&rsz=large";
    $.ajax({
        url: target,
        cache: false,                   
        dataType:'jsonp',
        success: function(data) {
            //alert(data.responseData.results);
            for(var x = 0; x < 8; x++){
                var title = data.responseData.results[x].titleNoFormatting;
                //var content = data.responseData.results[x].content;
                //var image = data.responseData.results[x].image.url;

                $("#home").append(x+"---"+title+'<hr>');
            }
        },
        error: function(jxhr,e){
        alert(jxhr.status+" --- "+e.responseText);
        }
    });
}

When I run the code this way I get 8 titles. 当我以这种方式运行代码时,我会得到8个标题。 but when I uncomment this line var image = data.responseData.results[x].image.url; 但是当我取消注释此行时var image = data.responseData.results[x].image.url; I get 3 or 4 results only. 我只得到3或4个结果。 I investigated the data being retrieved from google I found that some results has no images. 我调查了从Google检索到的数据,发现某些结果没有图像。 How can I check the json result if it has an image. 如何检查json结果是否有图片。 I still want to display the title of the article even if there was no image. 我仍然想显示文章的标题,即使没有图像也是如此。

you should check the image exists before get it's url 您应该在获取网址之前检查图片是否存在

if(typeof(data.responseData.results[x].image) == "undefined"){
    alert('no image');
}else{
    var image = data.responseData.results[x].image.url;
}

What does the response look like? 响应是什么样的? Most browsers have developer tools that will let you see the requests and response. 大多数浏览器都有开发人员工具,可让您查看请求和响应。 Likely, your loop is choking because data.responseData.results[x].image is undefined so data.responseData.result[x].image.url throws a type error (undefined doesn't have a url property). 可能是因为您的循环令人窒息,因为data.responseData.results[x].image未定义,所以data.responseData.result[x].image.url引发类型错误(未定义没有url属性)。

To guard against this, just check for it like this: 为了防止这种情况,只需检查如下:

if(data.responseData.result[x].image) {
    var image = data.responseData.result[x].image.url;
}

If data.responseData.result[x].image is undefined then it will evaluate as falsey and the body of the if won't be executed (and won't throw an error that breaks you out of the function). 如果data.responseData.result[x].imageundefined ,然后就被当作falsey和身体if不被执行(并不会抛出,打破你出功能的错误)。

You can check for image like 您可以检查像

if (typeof data.responseData.image != 'undefined' && typeof data.responseData.image.url != 'undefined' ) {
    // set the image here
}

Typeof can be used to determine if a variable is defined or not. Typeof可用于确定是否定义了变量。

Example

var var_one = "";

alert(typeof var_one); // alerts "string"
alert(typeof var_two); // alerts "undefined"
alert(typeof var_two == "undefined"); // alerts "true"

So you can specify: 因此,您可以指定:

if (typeof var_two != "undefined")
{
    alert("variable is undefined!");
}
else
{
    alert("variable is defined... use it!");
}

Taking the above into account, you could do: 考虑到上述因素,您可以执行以下操作:

var image = "";

if (typeof data.responseData.results[x].image.url != "undefined")
{
    image = data.responseData.results[x].image.url;

    // You can then specify where the image url will go.
}

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

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