Home > OS >  How can I convert string to JSON in Model class
How can I convert string to JSON in Model class

Time:10-25

I would like convert string to JSON while receiving the value from API. I am sending value from postman but I am unable to convert it to the JSON object in the model class ,I have decorated model class with the custom decorator. Thanks in Advance.

This is the Model Class and I wrote custom JSON convertor.

 namespace WebApplication2.Models
   {
    [Serializable]
    public class Test
    {
        [JsonConverter(typeof(UserConverter))]
        public AS_AscContext AscParcelContext { get; set; }

    }
    public class AS_AscContext
    {
        public string ShellType { get; set; }
        public string LayoutName { get; set; }
    }

   
    public class UserConverter : JsonConverter
    {
        private readonly Type[] _types;

        public UserConverter(params Type[] types)
        {
            _types = types;
        }

        public override void WriteJson(JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
        {
            JToken t = JToken.FromObject(value);

            if (t.Type != JTokenType.Object)
            {
                t.WriteTo(writer);
            }
            else
            {
                JObject o = (JObject)t;
                IList<string> propertyNames = o.Properties().Select(p => p.Name).ToList();

                o.AddFirst(new JProperty("Keys", new JArray(propertyNames)));

                o.WriteTo(writer);
            }
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
        {
            throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter.");
        }

        public override bool CanRead
        {
            get { return false; }
        }

        public override bool CanConvert(Type objectType)
        {
            return _types.Any(t => t == objectType);
        }
    }
   

This is the controller receiving value

 [HttpPost]
    public IActionResult Privacy([FromBody]Test aS_AggregatorRequest)
    {
       
        return View();
    }
      

    

This is the postman collection

CodePudding user response:

Try to change your postman JSON like this,

   {
      "ascParcelContext": {
          "shellType": "ceme-sales-wallaby",
          "layoutName": "value"
     }
   }

Don't escape any of the JSON data.

CodePudding user response:

your json has another json inside. So it is better to fix the api but if you don't have an access try this

[HttpPost]
public IActionResult Privacy([FromBody]JObject jsonObject)
{

jsonObject["ascParcelContext"]= JObject.Parse( (string) jsonObject["ascParcelContext"]);

Test test = jsonObject.ToObject<Test>();

.....

}

public class Test
{
    [JsonProperty("ascParcelContext")]
    public AS_AscContext AscParcelContext { get; set; }

}
public class AS_AscContext
{
    [JsonProperty("shellType")]
    public string ShellType { get; set; }
    [JsonProperty("layoutName")]
    public string LayoutName { get; set; }
}
  • Related