Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
{
  "imports": {
    "imported": [
      {
        "date": "19/9/2014",
        "item": [
          {
            "sn": "3366698",
            "type": "Food",
            "weight": "10tn."
          },
          {
            "sn": "3366699",
            "type": "Eqipment",
            "weight": "20kg."
          }
        ]
      },
      {
        "date": "20/9/2014",
        "item": [
          {
            "sn": "3366700",
            "type": "Electronics",
            "weight": "100pt."
          },
          {
            "sn": "3366701",
            "type": "Food",
            "weight": "5tn."
          }
        ]
      }
    ]
  }
}

I have this json and I am not sure if it's in the right structure. I am trying to render each item type (duplicates included) as table header by the following $.getJSON method:

$scope.items = data.imports.item;

and HTML as:

<table border="1" >
<thead>
<tr>
<th ng-repeat="item in items">{{item.type}}</th>
</tr>
</thead>
</table>

But I couldn't succeed. What am I doing wrong?

EDIT: jsfiddler

share|improve this question
    
jsfiddle plz.... –  itcouldevenbeaboat Sep 23 '14 at 13:04

2 Answers 2

Your json ist broken, paste your json here: http://jsonformatter.curiousconcept.com/

You'll see that data.imports.item would be undefined.

Correct JSON to access would look like:

{
  "imports": {

        "item": [
          {
            "sn": "3366698",
            "type": "Food",
            "weight": "10tn."
          },
          {
            "sn": "3366699",
            "type": "Eqipment",
            "weight": "20kg."
          }
        ]
      }
}

Also access your data after:

<th ng-repeat="item in items">
{{item["type"]}}
</th>
share|improve this answer
    
so where can I add imported date? –  Litestone Sep 23 '14 at 13:34
1  
You could try to nest your ng-repeat: <th ng-repeat="obj in imports["imported"]"> {{obj["date"]}} <span ng-repeat="item in obj"> {{item["type"] }} </span> </th> Reference to here: stackoverflow.com/questions/21161922/… –  graphefruit Sep 23 '14 at 13:49
    
I think I am getting closer –  Litestone Sep 23 '14 at 13:57

I don't see you are using the imported propery at all, try

$scope.items = data.imports.imported;

since imported is your array and not item.

<table border="1" >
<thead>
<tr>
<th ng-repeat="item in items">{{item.type}}</th>
</tr>
</thead>
</table>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.