2

Here's a sample of the JSON string :

    {
      "table": {
         "tfoot": "Footer",
         "tr0": [
                  {
                  "form": "formData",
                  "td": "Content"
                  }
                ]
       }
     }

And the jQuery code I'm using to parse it :

$.ajax({ 
    type: 'GET', 
    url: source, 
    dataType: 'json',
    success: function (data) { 

            $.each(data, function() {
              $.each(this, function(key, value) {
                switch (key) {
                    case "tfoot":
                        alert(value) // access to this node works fine                      
                    break;

                    default: 
                        alert(value.td) // this is undefined
                    break;
                }       
              });
            });
        }
    });

I tried a Console.log with Chrome and I can see every nodes and the data is okay. Anyone have a clue how I can access the "form" or "td" nodes?

2

3 Answers 3

1

The object value is an array, so you can not access the td property of it. If you wanted to get to the first item in the arrays td property you would need to do:

value[0].td

full code:

$.each(t, function() {
  $.each(this, function(key, value) {
    switch (key) {
      case "tfoot":
        console.log(value) // access to this node works fine                      
      break;

      default: 
        console.log(value[0].td) // this now prints "Content"
      break;
    }       
  });
 });
Sign up to request clarification or add additional context in comments.

Comments

0

value.table.tr0[0].td

Is what u are looking for.

Comments

0

in json { } defines an json object, [ ] defines an json Array.

So since after "tr0" comes a [ ] (array) you need to access it with an index. value.table.tr0[0].td should work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.