【问题标题】:jquery json parsing two codes one working other notjquery json解析两个代码一个工作另一个不
【发布时间】:2012-10-14 11:23:43
【问题描述】:

我有以下代码用于解析来自服务器的 jason 响应。我还包括各自的回应。第一个代码有效,但第二个代码无效。能给个提示吗?谢谢。 第一个代码:

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

【问题讨论】:

    标签: jquery ajax json rest jsonp


    【解决方案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']);
    

    获取文档编号:

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

    【讨论】:

    • 谢谢。你的建议有效。虽然如果我替换为:console.log(oData['ops:world-patent-data']['exchange-documents']['exchange-document']['bibliographic-data']['publication-reference'] ['doc-number)']);我收到未定义的消息,如何访问 doc-number 属性??
    【解决方案2】:

    如果我没看错的话,你会在结构中遗漏一个元素:

    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"].$);
    

    【讨论】:

    • 谢谢。现在我在控制台中收到未定义的消息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    相关资源
    最近更新 更多