2

I am using json.net to deserialize incoming json in a WebApi service.

var lines = JsonConvert.DeserializeObject<RootObject>(json);

After reading a number of other similar answers, it is still throwing this error:

The best overloaded method match for  'Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(string)' has some invalid arguments

Input json that is used in Postman for post:

{ "data": [
{"zipcode":"56033","line1":"610 4TH ST","city":"FROST","state":"MN","cid":"1770"},
{"zipcode":"56033","line1":"45375 30TH ST","city":"FROST","state":"MN","cid":"1771"},
{"zipcode":"56033","line1":"115 4TH ST","city":"FROST","state":"MN","cid":"1772"}
]}

The poco classes were created using: http://json2csharp.com .

  public class Datum
  {
    public string zipcode { get; set; }
    public string line1 { get; set; }
    public string city { get; set; }
    public string state { get; set; }
    public string cid { get; set; }
  }

  public class RootObject
  {
    public List<Datum> data { get; set; }
  }

Here is what I see when I debug the api:

{{
  "data": [
    {
      "zipcode": "56032-0056",
      "line1": "208 MAIN BOX 56",
      "city": "FREEBORN",
      "state": "MN",
      "cid": "1732"
    },
    {
      "zipcode": "56033",
      "line1": "610 4TH ST",
      "city": "FROST",
      "state": "MN",
      "cid": "1770"
    },
    {
      "zipcode": "56033",
      "line1": "45375 30TH ST",
      "city": "FROST",
      "state": "MN",
      "cid": "1771"
    },
    {
      "zipcode": "56033",
      "line1": "115 4TH ST",
      "city": "FROST",
      "state": "MN",
      "cid": "1772"
    },
    {
      "zipcode": "56033",
      "line1": "E 4TH ST",
      "city": "FROST",
      "state": "MN",
      "cid": "1773"
    }
  ]
}}

Not sure why webapi is adding an extra set of curly braces or how to prevent them.

Anyone see what I am missing?

5
  • @GillBates Sry, April 1st habit. json probably is what OP wrote as "Input json" Commented Apr 1, 2017 at 17:10
  • @Sentry I got that.. what is the variable json, you don't show it in the code. Commented Apr 1, 2017 at 17:10
  • @Sentry oh you're not even OP, lol. Commented Apr 1, 2017 at 17:11
  • 4
    Specifically, how is the variable (or member) json declared? Maybe it's not actually declared as a string. Commented Apr 1, 2017 at 17:15
  • It is the Input json described above. Commented Apr 2, 2017 at 2:56

1 Answer 1

5

If your json variable is a string, it will work. Here's a .NET Fiddle demonstrating.

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

4 Comments

The json variable is coming in from the api: public List<dynamic> PostAddressesByLine1Batch(string key, string product, [FromBody] dynamic data)
Ok, @kman gave the suggestion about the variable being a string. The current parameter is set to dynamic or the data comes through the api as null. So adding var json = data.ToString(); and then var lines = JsonConvert.DeserializeObject<RootObject>(json); correctly deserializes.
My error was to try to process a dynamic directly to Deserialize. You need to force it to string and the error stops happening. stackoverflow.com/questions/7827407/…
I also was trying to deserealize a dynamic directly. Just applied a .ToString() to the dynamic variable.

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.