2

I have two JSON formats,

JSON -

[
    { 
        "name":"Alex", 
        "country":"United States"
    }, 
    { 
        "name":"Jaswanth", 
        "country":"India"
    }
]

AngularJS Code -

I was able to display the output

<div ng-repeat="result in results">             
    {{result.name}} - {{result.country}}
</div>

But if I change my JSON, I am not able to see the output..

[
    {
        "info": [
            "one",
            "two",
            {
                "id": 944589,
                "contractYear": 2014
            }
        ],
        "country": "India",
        "name": "jaswanth"
    },
    {
        "info": [
            "three",
            "four",
            {
                "id": 944589,
                "contractYear": 2014
            }
        ],
        "country": "US",
        "name": "jass"
    }
]

How to I change my AngularJS code ?

9
  • 2
    JSON returned was originally an array, but now it's an object. Why did that change? Commented Nov 10, 2014 at 0:19
  • I have changed for the testing.. Commented Nov 10, 2014 at 0:21
  • If you do ng-repeat on the object it will iterate over each property instead so result.name would not exist, result itself would be the name. Commented Nov 10, 2014 at 0:22
  • 1
    Is this JSON still bound to a property named 'results'? Commented Nov 10, 2014 at 0:38
  • 2
    The same AngularJS template code should work fine on both structures (your very first, and your edited second). As such if this is not working then you have something else wrong somewhere which we cannot see (code, data format, an error, something...) Commented Nov 10, 2014 at 1:13

1 Answer 1

1
<div ng-init="init();">
    <div ng-repeat="result in data">             
        {{result.name}} - {{result.country}}
        <br/>
        <div ng-repeat="(k,v) in result.info">
            <span ng-if="v.id">The id is: {{v.id}}</span>
            <span ng-if="(!v.id)">{{v}}</span>
        </div>
        <hr/>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

3 Comments

info is essentially an array of objects, so treat it as an iterable
Thank you so much Coldstar, 90% completed. I am getting the output as.. jaswanth - india 0: one 1: two 2: {"id":944589,"contractYear":2014} jass - US 0: three 1: four 2: {"id":944589,"contractYear":2014} I want to print {"id":944589,"contractYear":2014} as normal values not json, can you help me to fix that.
I mean in the second index of 'info' array, we have to print for {{id}} - {{contractYear}}, i have tried to use ng-if but no luck.

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.