3

これが本当に基本的な質問である場合は申し訳ありませんが、解決しようとしている問題に似た例が見つかりません。

次のコードでネストされた要素の配列にアクセスできない理由と、その配列の要素にアクセスする方法を説明してください。以下のjsonから、2番目の結果から見つかった「Items」配列を取得できません。

次の json が返されます。

{
    "d": {
        "results": [
            {
                "__metadata": {
                    "uri": "...",
                    "type": "..."
                },
                "__index": 1,
                "ID": "someID1",
                "Name": "Some Name 1"
            },
            {
                "__index": 2,
                "Items": [
                    {
                        "__metadata": {
                            "uri": "...",
                            "type": "..."
                        },
                        "ID": "itemID2_1",
                        "Name": "Item 2_1"
                    }
                ]
            },
            {
                "__index": 3,
                "Items": [
                    {
                        "__metadata": {
                            "uri": "...",
                            "type": "..."
                        },
                        "ID": "itemID3_1",
                        "Name": "Item 3_1"
                    }
                ]
            },
            ...

翡翠のレイアウトは次のとおりです。

- var results=records, col_type='even';
table#results(style="border-collapse:collapse;")
  tr
    th.result-header Index
    th.result-header ID
    th.result-header Name
  - each r in results
    - col_type=col_type=='even' ? 'odd' : 'even'
    tr.result-row
      - if (!r.Items)
        td(class='result-col-'+col_type,style="border-left:1px solid black")
          #{r.__index}
        td(class='result-col-'+col_type,style="border-left:1px solid black")
          #{r.ID}
        td(class='result-col-'+col_type,style="border-left:1px solid black")
          #{r.Name}
      - else
        td(class='result-col-'+col_type,style="border-left:1px solid black")
          #{r.__index}
        - each i in r.Items
          td(class='result-col-'+col_type,style="border-left:1px solid black")
            #{i.ID}
          td(class='result-col-'+col_type,style="border-left:1px solid black")
            #{i.Name}
4

2 に答える 2

0

ここでの問題は、JSON がこの形式であることです。

{
  "d": { 
     "results": [
        ...
      ]

したがって、jade テンプレートのこの部分を次のように変更する必要があります。

- each r in results
  - col_type=col_type=='even' ? 'odd' : 'even'

これに、

- each r in results['d']['results']
  - col_type=col_type=='even' ? 'odd' : 'even'

このようにして、ループは各配列項目を通過します。

于 2011-09-06T18:39:19.257 に答える