如何使用jquery读取json值?

时间:2012-10-28 21:04:22

标签: javascript jquery json

  

可能重复:
  I have a nested data structure / JSON, how can access a specific value?

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

我在下面有以下JSON提要,我希望能够读取和输出名称,标题,类型,ID,电子邮件值等所有值 我对所有人都没有定义

///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;
  });
});

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

1 个答案:

答案 0 :(得分:0)

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

json.property.nestedProperty.value