解析JSON缺失数据会导致错误

时间:2013-01-11 03:44:17

标签: 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",
        "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 "
    }
}

有效节点的后续访问将出错。消除丢失数据的请求,随后的请求将起作用。必须有一种方法来测试对象,然后才能使用它,这样就不会为数据提供FUBAR。

这个数据没问题

//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."
    }
}

}

2 个答案:

答案 0 :(得分:2)

错误不会导致其他代码失败。当第一行出错时,它会停止执行,以便永远不会到达其他代码。

您可以添加以下内容:

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

答案 1 :(得分:0)

如果你的json字段没有修复,你必须检查它们在json结果中的存在。 您可以尝试下面的代码来做同样的事情:

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

OR

制作如下所示的函数并传递node元素以验证其存在(首选)

function isExists(node) {
    if (node== undefined)
      return ""
    else
      return node
}

//并像这样称呼它

var nodevalue=isExists(json.track.wiki)

通过这样做,您确保在使用之前存在特定节点。以便以后的代码也能正确执行。

您可能会在上面执行代码时遇到一些错误。所以别忘了纠正!!