0

After much wrangling, I finally got my Json result to work properly in my web api for classes with associations:

    public string GetAll()
    {
        var order =_repository.GetAll();
        var settings = new JsonSerializerSettings
               {
                   ReferenceLoopHandling = ReferenceLoopHandling.Ignore
               };

        return JsonConvert.SerializeObject(order, settings);

    }

But when I test in, either in browser or Fiddler, instead of something typical like:

(A)

     "Books":
        [  
           {    
             "Id": "1",  
             "Name": "Book1", 
             "Authors": 
                [
                  { 
                    ....... 
                  }
               ]  
           } 
       ] 

(B) I get this:

"[{\"Id\":1,\"Name\":\"Book1\",\"Authors\":[{\"Id\":1,\"PersonId\":1,\"Person\":null,\"Books\":[{\"Id\":3,\"Name\":\"Book3\",\"Authors\":[{\"Id\":4,\"PersonId\":4,\"Person\":null,\"Books\":[{\"Id\":2,\"Name\":\"Book2\",\"Authors\":[{\"Id\":2,\"PersonId\":2,\"Person\":null,\"Books\": .....

Can someone kindly tell me what I must do to format it like A. above?

Thanks

Update:

BTW, Formatting.Indented made it worse:

"[\r\n {\r\n \"Id\": 1,\r\n \"Name\": \"Book1\",\r\n \"Authors\": [\r\n {\r\n \"Id\": 1,\r\n \"PersonId\": 1,\r\n \"Person\": {\r\n \"Id\": 1,\r\n \"FirstName\": \"John\",\r\n \"LastName\": \"Doe\"\r\n },\r\n \"Books\": [\r\n {\r\n \"Id\": 3,\r\n \"Name\": \"Book3\",\r\n \"Authors\": [\r\n {\r\n \"Id\": 4,\r\n \"PersonId\": 4,\r\n \"Person\": {\r\n \"Id\": 4,\r\n \"FirstName\": \"Julie\",\r\n

3 Answers 3

2

The traditional approach is that your API controller returns either an HttpResponseMessage or a collection object, but not the already serialised object. The MediaTypeFormatter objects will take care of that for you.

What looks to be happening is that your controller is returning a string which is then being converted into a JSON representation (because of the MediaTypeFormatter for JSON). Try changing your controller to just return the object collection (without serialising it to a string) and it should work for you.

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

Comments

0

settings.Formatting = Formatting.Indented;

just curious...any reason why you are using JsonMediaTypeFormatter to handle writing the response?...i see that you are serializing it explicitly to json...

1 Comment

I would say that it depends on the requirement. Do you want to support even sending data back in Xml format? Do you want to always send data back in Json? if you would want to send data back in Xml too, then you can let the content-negotiation process decide about which formatter can handle writing the Order object...even if you want to always send data explicitly in Json, you could use JsonMediaTypeFormatter directly also..
0

I think the issue here is that the JSON is being serialized twice, once by you, and again by WebAPI. Remove your serialization and all should be fine.

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.