Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 解析JSON以查找丢失的数据会导致错误_Jquery_Json - Fatal编程技术网

Jquery 解析JSON以查找丢失的数据会导致错误

Jquery 解析JSON以查找丢失的数据会导致错误,jquery,json,Jquery,Json,如果这与我之前的问题类似,我很抱歉。为什么搜索JSON解析中不存在的节点会导致其他所有有效节点失败 json.track.wiki.summary causes all to error json.track.name is fine if used without the previous //JSON data { "track": { "id": "434483410", "name": "Written in the Stars",

如果这与我之前的问题类似,我很抱歉。为什么搜索JSON解析中不存在的节点会导致其他所有有效节点失败

 json.track.wiki.summary causes all to error
 json.track.name  is fine if used without the previous

//JSON data
{
    "track": {
        "id": "434483410",
        "name": "Written in the Stars",
        "mbid": "",
        "url": "http://www.last.fm/music/Tinie+Tempah+(Feat.+Eric+Turner)/_/Written+in+the+Stars",
        "duration": "208000",
        "streamable": {
            "#text": "0",
            "fulltrack": "0"
        },
        "listeners": "108",
        "playcount": "1523",
        "artist": {
            "name": "Tinie Tempah (Feat. Eric Turner)",
            "mbid": "",
            "url": "http://www.last.fm/music/Tinie+Tempah+(Feat.+Eric+Turner)"
        },
        "toptags": "\n "
    }
}
对有效节点的后续访问将出错。消除丢失数据的请求,后续请求将起作用。在使用对象之前,必须有一种方法来测试它,这样它就不会破坏数据

这个数据就可以了

//JSON Data
{
"track": {
    "id": "517047006",
    "name": "Skyscraper",
    "mbid": "a92f853d-ad6d-490e-a820-4cff9cc1f224",
    "url": "http://www.last.fm/music/Demi+Lovato/_/Skyscraper",
    "duration": "222000",
    "streamable": {
        "#text": "1",
        "fulltrack": "0"
    },
    "listeners": "114757",
    "playcount": "1424456",
    "artist": {
        "name": "Demi Lovato",
        "mbid": "faf4cefb-036c-4c88-b93a-5b03dd0a0e6b",
        "url": "http://www.last.fm/music/Demi+Lovato"
    },
    "album": {
        "artist": "Demi Lovato",
        "title": "Unbroken",
        "mbid": "9c195a9b-2db4-4b63-9337-6d8152244742",
        "url": "http://www.last.fm/music/Demi+Lovato/Unbroken",
        "image": [{
            "#text": "http://userserve-ak.last.fm/serve/64s/69672054.png",
            "size": "small"
        }, {
            "#text": "http://userserve-ak.last.fm/serve/126/69672054.png",
            "size": "medium"
        }, {
            "#text": "http://userserve-ak.last.fm/serve/174s/69672054.png",
            "size": "large"
        }, {
            "#text": "http://userserve-ak.last.fm/serve/300x300/69672054.png",
            "size": "extralarge"
        }],
        "@attr": {
            "position": "11"
        }
    },
    "toptags": {
        "tag": [{
            "name": "pop",
            "url": "http://www.last.fm/tag/pop"
        }, {
            "name": "ballad",
            "url": "http://www.last.fm/tag/ballad"
        }, {
            "name": "female vocalists",
            "url": "http://www.last.fm/tag/female%20vocalists"
        }, {
            "name": "inspirational",
            "url": "http://www.last.fm/tag/inspirational"
        }, {
            "name": "demi lovato",
            "url": "http://www.last.fm/tag/demi%20lovato"
        }]
    },
    "wiki": {
        "published": "Sat, 20 Aug 2011 18:25:23 +0000",
        "summary": "The Skyscraper Songfacts says: On November 1, 2010, Demi's publicist announced the Disney star had entered a treatment facility for "
        physical and emotional issues,
        " which was subsequently reported to include an eating disorder and self-cutting. Her first single since her stint in treatment finds the singer/actress finding strength in the wake of a fading relationship and it symbolizes her resilience in the face of her personal issues.",
        "content": "The Skyscraper Songfacts says: On November 1, 2010, Demi's publicist announced the Disney star had entered a treatment facility for "
        physical and emotional issues,
        " which was subsequently reported to include an eating disorder and self-cutting. Her first single since her stint in treatment finds the singer/actress finding strength in the wake of a fading relationship and it symbolizes her resilience in the face of her personal issues.\n \nUser-contributed text is available under the Creative Commons By-SA License and may also be available under the GNU FDL."
    }
}

}

该错误不会导致其他代码失败。当第一行出错时,它将停止执行,从而永远无法到达其他代码

您可以添加如下内容:

if(json && json.track && json.track.wiki){
    // do something with json.track.wiki.summary
}

如果您的json字段不固定,您必须检查json结果中是否存在这些字段。 您可以尝试以下代码来执行相同的操作:

if (typeof(json.track.wiki) != "undefined")
{
//code for what to do with json here
}

创建一个如下所示的函数,并传递node元素以验证其存在性(首选)

//这样称呼它

var nodevalue=isExists(json.track.wiki)
通过这样做,您可以确保在使用特定节点之前该节点已存在。这样以后的代码也能正确执行


执行上面的代码时可能会出现一些错误。所以别忘了纠正

您是否使用var data=$.parseJSON(源代码)?。您的第二组数据不是有效的JSONYes:var obj=jQuery.parseJSON(strLastFM);我一直使用JSON,因为它来自Last.FM,任何查询都会有所不同。
var nodevalue=isExists(json.track.wiki)