0

可能重复:
我有一个嵌套数据结构/JSON,如何访问特定值?

我正在尝试读取一些 json 并显示它们的值。

我在下面有以下 JSON 提要,我希望能够读取和输出 name、caption、type、id、email 值等的所有值,我对所有这些值都没有定义

///html 
<div id="results"></div>

我的 json 看起来像这样

{
    "action":"index.html",
    "method":"post",
    "html":[
        {
            "type":"fieldset",
            "caption":"User information",
            "html":[
                {
                    "name":"email",
                    "caption":"Email address",
                    "type":"text",
                    "placeholder":"E.g. user@example.com",
                    "validate":{
                        "email":true
                    }
                },
                {
                    "name":"password",
                    "caption":"Password",
                    "type":"password",
                    "id":"registration-password",
                    "validate":{
                        "required":true,
                        "minlength":5,
                        "messages":{
                            "required":"Please enter a password",
                            "minlength":"At least {0} characters long"
                        }
                    }
                },
                {
                    "name":"password-repeat",
                    "caption":"Repeat password",
                    "type":"password",
                    "validate":{
                        "equalTo":"#registration-password",
                        "messages":{
                            "equalTo":"Please repeat your password"
                        }
                    }
                },
                {
                    "type":"radiobuttons",
                    "caption":"Sex",
                    "name":"sex",
                    "class":"labellist",
                    "options":{
                        "f":"Female",
                        "m":"Male"
                    }
                },
                {
                    "type":"checkboxes",
                    "name":"test",
                    "caption":"Receive newsletter about",
                    "class":"labellist",
                    "options":{
                        "updates":"Product updates",
                        "errors":{
                            "value":"security",
                            "caption":"Security warnings",
                            "checked":"checked"
                        }
                    }
                }
            ]
        },
        {
            "type":"fieldset",
            "caption":"Address information",
            "html":[
                {
                    "name":"name",
                    "caption":"Your name",
                    "type":"text",
                    "placeholder":"E.g. John Doe"
                },
                {
                    "name":"address",
                    "caption":"Address",
                    "type":"text",
                    "validate":{ "required":true }
                },
                {
                    "name":"zip",
                    "caption":"ZIP code",
                    "type":"text",
                    "size":5,
                    "validate":{ "required":true }
                },
                {
                    "name":"city",
                    "caption":"City",
                    "type":"text",
                    "validate":{ "required":true }
                },
                {
                    "type":"select",
                    "name":"continent",
                    "caption":"Choose a continent",
                    "options":{
                        "america":"America",
                        "europe":{
                            "selected":"true",
                            "id":"europe-option",
                            "value":"europe",
                            "html":"Europe"
                        },
                        "asia":"Asia",
                        "africa":"Africa",
                        "australia":"Australia"
                    }
                }
            ]
        },
        {
            "type":"submit",
            "value":"Signup"
        }
    ]

}

我的代码如下所示:

$(document).ready(function() {
  $('#letter-b a').click(function() {
    $.getJSON('dformexternal.json', function(data) {
      $('#results').empty();
      $.each(data, function(entryIndex, entry) {
        var html = '<div class="entry">';
        html += '<h3 class="html">' + entry['html'] + '</h3>';
        html += '<div class="name">' + entry['name'] + '</div>';
         html += '<div class="caption">' + entry['caption'] + '</div>';
          html += '<div class="type">' + entry['type'] + '</div>';
        html += '<div class="definition">';
        html += entry['definition'];
        if (entry['name']) {
          html += '<div class="name">';
          $.each(entry['name'], function(lineIndex, line) {
            html += '<div class="name">' + line + '</div>';
          });
          if (entry['caption']) {
            html += '<div class="caption">' + entry['caption'] + '</div>';
          }
          html += '</div>';
        }
        html += '</div>';
        html += '</div>';
        $('#results').append(html);
      });
    });
    return false;
  });
});

我似乎无法访问任何值。我做错了什么?任何帮助都会很棒。

4

1 回答 1

0

您可以将 json 值作为常规属性访问。

json.property.nestedProperty.value

于 2012-10-28T21:06:48.523 回答