2

my data.json looks like this:

{
    "selection_form" : {
        "entities" : { 
            "name":"0002" ,
            "name":"0103" ,
            "name":"0104" ,
            "name":"0122" ,
....

this script looks loke this

  <script>     
            $.getJSON('data.json', function(data) {   
                $.each(data.selection_form.entities, function(i,item){
                    $("#enity").append('<p>'+data.selection_form.entities.name+'</p>');
                });
            });
        </script>

i want to get all names wraped in a p tag like

<p>0002</p><p>0103</p>....

but the result is ony the last name item. I can't find a soloution for that. Need help!

0

2 Answers 2

1

An object can't have properties with same names. entities object has one name property with value of 0122. You should change the property names.

{
    "selection_form": {
        "entities": {
            "name1": "0002",
            "name2": "0103",
            "name3": "0104",
            "name4": "0122",
        }
    }
}

$.getJSON('data.json', function(data) {   
      $.each(data.selection_form.entities, function(i,item){
           $("#enity").append('<p>'+item+'</p>');
      });
 });

http://jsfiddle.net/vB6qe/

Sign up to request clarification or add additional context in comments.

Comments

0

You are overriding the JSON property names. Instead of having a JSON object there, it makes more sense to have an array:

{
    "selection_form" : {
        "entities" : [ "0002" , "0103" , "0104" , "0122" ]
    }
}


<script type="text/javascript">
    $.getJSON('data.json', function(data) {   
        $.each(data.selection_form.entities, function(i,name){
            $("#enity").append('<p>'+name+'</p>');
        });
    });
</script>

Here's a DEMO of iterating that structure.

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.