0

如果这可能与我之前的问题相似,我深表歉意。为什么搜索 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."
    }
}

}

4

2 回答 2

2

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

您可以添加如下内容:

if(json && json.track && json.track.wiki){
    // do something with json.track.wiki.summary
}
于 2013-01-11T04:06:24.100 回答
0

如果您的 json 字段不固定,您必须检查它们在 json 结果中是否存在。你可以尝试下面的代码来做同样的事情:

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

或者

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

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

//并像这样调用它

var nodevalue=isExists(json.track.wiki)

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

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

于 2013-01-11T04:31:54.663 回答