TransWikia.com

Deserializing json C# unexpected character encountered

Stack Overflow Asked by kjenn31 on November 4, 2021

Not sure where I’m going wrong here. Serialized an object with newtonsoft to post to a web api. Running into error: "unexpected character encountered"

Here’s my json:

{
"Eve": "wizz",
"familyid": "1006949",
"time": "07/21 05:45:59 AM",
"DogName": "Rocky",
"userid": "1006949"
}

Deserialize:

BtnData listObj = Newtonsoft.Json.JsonConvert.DeserializeObject<BtnData>(json.ToString());

Model:

   public class BtnData
    {

        [JsonProperty("Eve")]
        public string Eve { get; set; }
        [JsonProperty("Familyid")]
        public string Familyid { get; set; }
        [JsonProperty("Time")]
        public string Time { get; set; }
        [JsonProperty("DogName")]
        public string DogName { get; set; }
        [JsonProperty("Userid")]
        public string Userid { get; set; }
    }

controller:

public ActionResult AddEvent([FromBody] BtnData json)

One Answer

Solved by @knoop. Thank you !

json is already deserialized for you (it does that for you since you declared [FromBody] BtnData json. When you call ToString() on it it falls back to the default implementation which, for a class, just prints the name of it's type. And this name is invalid json so deserialization fails

Answered by kjenn31 on November 4, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP