1

I'm making a little site to manage recipes. I have an MVC web api server side and an angular js front end.

Now my MVC web api is serializing JSON as (clean and readable, thank you chrome plugin JSONView)

[
{
$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"
}
]

But on my page, I only get the first object in my list and an empty second tr tag. Is there a way to make AngularJS understand the $ref reference? Or should I tinker with my web api config?

config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(
    config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml"));

var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
json.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

config.Formatters.Remove(config.Formatters.XmlFormatter);

- UPDATE

The json above is a pretty representation by a chrome extension. Below is the raw data I get from my service (and formatted with JSONLint, which said it's valid JSON):

[
    {
        "$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"
    }
]
2
  • 1
    Your JSON is messed up - is that really what you get from serialization? Commented Jan 22, 2014 at 15:55
  • the JSON is not stringified. Commented Jan 22, 2014 at 15:57

2 Answers 2

1

Your JSON is invalid, which means the problem is on the API side.

Category: {
  $ref: "2"
}
Id: 2
...
  ],
  Id: 1
}
Id: 1

There needs to be a comma before Id: 2 and Id: 1 for this to be parsed successfully.

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

Comments

0

The guys from the api have updated their code so loops now no longer occur. Which fixed my problem.

Thanks anyway! :)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.