angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

JSON not parsed correctly

Open KenBonny opened this issue 12 years ago • 2 comments

Hello,

I get JSON from a web service (about recipes and ingredients) and one recipe uses multiple ingredients. Now when I query the service for all the ingredients, I get the following JSON back:

[{"id": "1","Name": "Spaghetti","CookTime": "00:30:00","Servings": 2,"CategoryId": 1,"Category": {"$id": "2","Name": "Pasta","Recipes": [{"$ref": "1"},{"$id": "3","Name": "Recipe 2","CookTime": "01:20:00","Servings": 2,"CategoryId": 1,"Category": {"$ref": "2"},"Id": 2}],"Id": 1},"Id": 1},{"$ref": "3"}]

The problem is that angular doesn't parse the "$ref" correctly and it shows an empty option. See below fiddle for result.

http://jsfiddle.net/FQk3u/1/

Is there a way around this? Is this a problem with JS in general or is this specific to AngularJS?

KenBonny avatar Feb 05 '14 09:02 KenBonny

Hi KenBonny,

I think you need to check your JSON. In the fiddle you linked you are looping through items and trying to print the name attribute. Your data has 2 elements, and the 2nd element has no name attribute.

[
   {
      "id":"1",
      "Name":"Spaghetti",
      "CookTime":"00:30:00",
      "Servings":2,
      "CategoryId":1,
      "Category":{
         "$id":"2",
         "Name":"Pasta",
         "Recipes":[
            {
               "$ref":"1"
            },
            {
               "$id":"3",
               "Name":"Recipe 2",
               "CookTime":"01:20:00",
               "Servings":2,
               "CategoryId":1,
               "Category":{
                  "$ref":"2"
               },
               "Id":2
            }
         ],
         "Id":1
      },
      "Id":1
   },
   {
      "$ref":"3"
   }
]

Substr avatar Feb 05 '14 10:02 Substr

@KenBonny the remark from @Courthold is correct - your JSON doesn't have any Name attribute for the second item. Not sure what you mean by "JSON not being parsed correctly" as we can't see your web service output. Without it it is impossible to move on on this topic.

I'm going to close this issue for now, feel free to re-open when you've got more info.

pkozlowski-opensource avatar Feb 05 '14 20:02 pkozlowski-opensource