jquery json解析两个代码一个工作其他没有

时间:2012-10-24 12:07:43

标签: jquery ajax json rest jsonp

我有以下代码用于解析来自服务器的jason响应。我还包括各自的答复。第一个代码可以工作,但第二个代码没有。你能暗示一下吗?谢谢。 第一个代码:

<html>
<head>
<script type="text/javascript" 

2 个答案:

答案 0 :(得分:1)

您不想迭代bibliographic-data的内容,只是从中获取一个属性

替换此

$.each(oData['ops:world-patent-data']['exchange-documents']['bibliographic-data'],
    function(iIndex, mValue) {
         console.log(mValue['publication-reference'])
    }
);

与此:

console.log(oData['ops:world-patent-data']['exchange-documents']['bibliographic-data']['publication-reference']);

获取doc-number:

console.log(oData['ops:world-patent-data']['exchange-documents']['bibliographic-data']['publication-reference']['document-id'][0]['doc-number']);

答案 1 :(得分:0)

如果我正确阅读,你在结构中缺少一个元素:

ops:world-patent-data.exchange-documents.exchange-document.bibliographic-data.publication-reference

所以插入['exchange-document']

将其放入小提琴文档中,看看这是否对您有所帮助:http://jsfiddle.net/pfJjG/1/

var iam = {
    "ops:world-patent-data": {
        "@xmlns": {
            "ops": "http://ops.epo.org",
            "$": "http://www.epo.org/exchange",
            "ccd": "http://www.epo.org/ccd",
            "xlink": "http://www.w3.org/1999/xlink"
        },
        "ops:meta": {
            "@name": "elapsed-time",
            "@value": "31"
        },
        "exchange-documents": {
            "exchange-document": {
                "@system": "ops.epo.org",
                "@family-id": "35636806",
                "@country": "EP",
                "@doc-number": "1814517",
                "@kind": "A1",
                "bibliographic-data": {
                    "publication-reference": {
                        "document-id": [
                            {
                            "@document-id-type": "docdb",
                            "country": {
                                "$": "EP"
                            },
                            "doc-number": {
                                "$": "1814517"
                            },
                            "kind": {
                                "$": "A1"
                            },
                            "date": {
                                "$": "20070808"
                            }},
                        {
                            "@document-id-type": "epodoc",
                            "doc-number": {
                                "$": "EP1814517"
                            },
                            "date": {
                                "$": "20070808"
                            }}
                        ]
                    },
                    "classifications-ipcr": {
                        "classification-ipcr": [
                            {
                            "@sequence": "1",
                            "text": {
                                "$": "A61K   9/    08            A I"
                            }},
                        {
                            "@sequence": "2",
                            "text": {
                                "$": "A61K  31/    19            A I"
                            }},
                        {
                            "@sequence": "3",
                            "text": {
                                "$": "A61K  31/   216            A I"
                            }},
                        {
                            "@sequence": "4",
                            "text": {
                                "$": "A61K  47/    00            A I"
                            }}]
                    }
                }
            }
        }
    }
};
$('#me').text(iam["ops:world-patent-data"]["exchange-documents"]["exchange-document"]["bibliographic-data"]["publication-reference"]["document-id"][0]["doc-number"].$);