2

I'm fairly new to Web API and have already created a successful API, but I am now working with different JSON and I don't know how exactly Web API parses JSON so nicely. Before, I had JSON like this:

[
  {
    "email": "[email protected]",
    "timestamp": 1337197600,
    "id": "55555",
  }
]

And my Web API Method signature was:

[HttpPost]
public HttpResponseMessage Create(List<MyOwnModel> data)
{
    ...
}

This worked great. I had a list of all of the JSON objects and all of the attributes associated with it. Now I have JSON like this:

    randomJsonObjects: 
    [
      {
        "email": "[email protected]",
        "timestamp": 1337197600,
        "id": "55555",
      }
    ]

Well that randomJsonObjects broke it. Now data is null when I hit the API. What am I missing?

1
  • Rename the data variable to randomJsonObjects Commented Apr 4, 2014 at 23:26

2 Answers 2

2

For example, for the following model, the below json would be the right one. Notice the root level { and } characters that you are missing from your payload and also action's parameter.

Model:

public class AddressBook
{
    public List<ContactInfo> Contacts {get; set;}
}

Controller:

[HttpPost]
public HttpResponseMessage Create(AddressBook book)
{
}

JSON:

{
    Contacts: 
    [
      {
        "email": "[email protected]",
        "timestamp": 1337197600,
        "id": "55555",
      }
    ]
}
Sign up to request clarification or add additional context in comments.

Comments

1

I see a few issues, there are different ways you could go about resolving them but I'll suggest the simplest. The json you want isn't valid. If it were contained within an object it would be fine, if you want just an array that's fine to. But you can't have a property on it's own without being contained by an object which is what the below sample is.

   randomJsonObjects: 
    [
      {
        "email": "[email protected]",
        "timestamp": 1337197600,
        "id": "55555",
      }
    ]

Your method; public HttpResponseMessage Create(List<MyOwnModel> data) is expecting a list, so one option would be to change this to something like;

 public HttpResponseMessage Create(MyRespObj data)

and then have;

 public class MyRespObj
 {
       List<MyOwnModel> randomJsonObjects;
 }

Now that would produce a response of;

{
   randomJsonObjects: 
    [
      {
        "email": "[email protected]",
        "timestamp": 1337197600,
        "id": "55555",
      }
    ]
}

which would be valid. The other option is to just send the array. Either way you can't just return a property without it being part of an object. If you take that json and say try it on something like http://jsonlint.com/ you will find it's not valid and that is really the problem.

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.