Web API JSON deserialization converting value issue

asp.net-web-api entity-framework-6 json.net

Question

I am trying to insert JSON data into my models via EF 6. I am getting this error;

    Inner Exception is {0}System.ArgumentException: Could not cast or convert from System.String to EPINMiddleWareAPI.Models.Serial.
   konum: Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType)
   konum: Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType)
   konum: Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)
Message ---
{0}Error converting value "c57a55b5-b2d4-46e7-86e0-cc13726ca8a7" to type 'EPINMiddleWareAPI.Models.Serial'. Path 'coupons[0].serials[0]', line 1, position 310.

Here is my model structure:

public class ConfirmResponse
    {

        public int Id { get; set; }
        public string referenceId { get; set; }
        public string version { get; set; }
        public string signature { get; set; }
        public string paymentID { get; set; }
        public string productCode { get; set; }
        public string currency { get; set; }
        public string ApplicationCode { get; set; }
        public double unitPrice { get; set; } 
        public double totalPrice { get; set; }
        public string purchaseStatusCode { get; set; }
        public DateTime? purchaseStatusDate { get; set; }
        public DateTime? requestDateTime { get; set; } = DateTime.Now;
        public string merchantProductCode { get; set; }
        public List<Coupon> coupons { get; set; }

    }

 public class Coupon
    {

        public int Id { get; set; }
        public int ConfirmResponseID { get; set; }
        public List<Serial> serials { get; set; }
        public List<Pin> pins { get; set; }
        public virtual ConfirmResponse confirmResponse { get; set; }

    }

public class Serial
    {
        public int Id { get; set; }
        public string serials { get; set; }
        public int CouponID { get; set; }
        public virtual Coupon coupons { get; set; }
    }

My sample JSON:

"{\"referenceId\":\"0000000046\",\"paymentId\":\"MDO1037624\",\"productCode\":\"001002461285\",\"quantity\":\"1\",\"currency\":\"TL\",\"unitPrice\":\"46,47\",\"totalPrice\":\"46,47\",\"merchantProductCode\":\"\",\"purchaseStatusCode\":\"00\",\"purchaseStatusDate\":\"2019-03-17T20:58:48Z\",\"coupons\":[{\"serials\":[\"c57a55b5-b2d4-46e7-86e0-cc13726ca8a7\"],\"pins\":[\"T6VC-XC6X-3H6JS-NVWL-93PCa\"]}],\"version\":\"V1\",\"signature\":\"2f961c7dbc32c3bc128b6e69d19e8e1a\",\"applicationCode\":\"52e7cf966b724749a7c4efadc3727ed7\"}"

How can I fix this problem? I would like to insert this JSON into my database tables. I am using EF 6.

edit: This model structure solved my problem. Now I can insert serials and pins into database.

public class ConfirmResponse
    {

        public int Id { get; set; }
        public string referenceId { get; set; }
        public string version { get; set; }
        public string signature { get; set; }
        public string paymentID { get; set; }
        public string productCode { get; set; }
        public string currency { get; set; }
        public string ApplicationCode { get; set; }
        public double unitPrice { get; set; } 
        public double totalPrice { get; set; }
        public string purchaseStatusCode { get; set; }
        public DateTime? purchaseStatusDate { get; set; }
        public DateTime? requestDateTime { get; set; } = DateTime.Now;
        public string merchantProductCode { get; set; }
        public List<Coupon> coupons { get; set; }

    }
public class Coupon
    {

        public int Id { get; set; }
        public int ConfirmResponseID { get; set; }
        public List<string> serials { get; set; }
        public List<string> pins { get; set; }
        public virtual ConfirmResponse confirmResponse { get; set; }
        public List<string> StringsSerial
        {
            get { return serials; }
            set { serials = value; }
        }
        public List<string> StringsPin
        {
            get { return pins; }
            set { pins = value; }
        }

        [Required]
        public string Serial
        {
            get { return String.Join(",", serials); }
            set { serials = value.Split(',').ToList(); }
        }
        [Required]
        public string Pin
        {
            get { return String.Join(",", pins); }
            set { pins = value.Split(',').ToList(); }
        }
    }

Thanks in advance.

1
0
3/18/2019 6:15:25 PM

Popular Answer

you're json is wrong

take a look into the coupons array, you initialize the serials array with an string/guid and not with a model of serial

it should be

"coupons": [{
        {   
            "serials": [{
                "serials": "c57a55b5-b2d4-46e7-86e0-cc13726ca8a7"
            }
        ],
        }
    }
]...

I guess for Pins the same

0
3/18/2019 12:07:57 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow