0

Possible Duplicate:
I have a nested data structure / JSON, how can access a specific value?

I am trying to read some json and displays their values .

I have the following JSON feed below and I want to be able to read and output all the value of name , caption, type, id , email values etc I am getting undefined for all of them

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

My json looks like this

{
    "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"
        }
    ]

}

My code look like this:

$(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;
  });
});

I cant seem to access any of the values. What have I done wrong? Any help would be great.

Community
  • 1
  • 1
user244394
  • 13,168
  • 24
  • 81
  • 138
  • 2
    I will sugget to put a break point in success handler and debug your code. Use FireBug in Firefox for example. – Reflective Oct 28 '12 at 21:06
  • Or even use `console.log` to see how far the loops get – Ian Oct 28 '12 at 21:08
  • 1
    Manual concatenation of HTML source code is painful to watch `-.-` Check out Handlebars. – Šime Vidas Oct 28 '12 at 21:09
  • at least start at the correct level of the object. `data.html` is the first big array for your `each`. There is deeper nesting inside that array than your code allows for also – charlietfl Oct 28 '12 at 22:22
  • if i use data.html, how would i go about accessing each on for the values for name, caption, type etc? – user244394 Oct 28 '12 at 22:25

1 Answers1

0

You can access json values as regular properties.

json.property.nestedProperty.value

Paul Knopf
  • 9,568
  • 23
  • 77
  • 142