1

I have a json string returned from an api endpoint and am trying to deserialize it to a c# class. The deserialization process is erroring out.

incoming json string:

{"amount":"3.00","resptext":"Approval","cvvresp":"P","respcode":"000","avsresp":"","merchid":"800000000800","token":"9478837814450119","authcode":"PPS306","respproc":"RPCT","emvTagData":"{\"TVR\":\"0200008000\",\"PIN\":\"None\",\"Signature\":\"true\",\"Mode\":\"Issuer\",\"ARC\":\"Z3\",\"TSI\":\"E800\",\"Application Preferred Name\":\"CREDITO DE VISA\",\"AID\":\"A0000000031010\",\"IAD\":\"06010A03A00000\",\"Entry method\":\"Chip Read\",\"Application Label\":\"VISA CREDIT\"}","retref":"116390139157","respstat":"A","account":"47XXXXXXXXXX0119"}

c# class:

public class AuthCardResponse
    {      
        public string token { get; set; }
        public string expiry { get; set; }
        public string name { get; set; }
        public string batchid { get; set; }
        public string retref { get; set; }
        public string avsresp { get; set; }
        public string respproc { get; set; }
        public string amount { get; set; }
        public string resptext { get; set; }
        public string authcode { get; set; }
        public string respcode { get; set; }
        public string merchid { get; set; }
        public string cvvresp { get; set; }
        public string respstat { get; set; }
        public string account { get; set; }
        public string bintype { get; set; }
        public string entrymode{get;set;}
        public string commcard { get; set; }        
        //public string emvTagData { get; set; }        
        public EmvTagData emvTagData { get; set; }
    }    

    public class EmvTagData
    {
        public string TVR { get; set; }
        public string PIN { get; set; }
        public string Signature { get; set; }
        public string Mode { get; set; }
        public string TSI { get; set; }
        public string AID { get; set; }
        public string ARC { get; set; }
        public string IAD { get; set; }

        [JsonProperty("Entry method")]
        public string Entrymethod { get; set; }

        [JsonProperty("Application Label")]
        public string ApplicationLabel { get; set; }

        [JsonProperty("Application Preferred Name")]
        public string ApplicationPreferredName { get; set; }
    }
}

code to deserialize:

AuthCardResponse _authCardResponse = JsonConvert.DeserializeObject<AuthCardResponse>(authResultJson );

Error:

at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)

I can't figure out what the error is. I have tried online json parsers and they have been able to parse the string without any issues.

Any help is appreciated.

Thanks NH

6
  • 2
    What's the error message you get? What you've labelled as the error is actually the stack trace. Commented Apr 26, 2019 at 15:02
  • Error Message Error reading string. Unexpected token: StartObject. Path '', line 1, position 1. Commented Apr 26, 2019 at 15:05
  • Looks like .net is throwing the exception Commented Apr 26, 2019 at 15:06
  • Have you tried removing all the properties from the class and adding them in one by one to see at which point it breaks? You should also remove the corresponding property from the JSON string Commented Apr 26, 2019 at 15:10
  • 2
    looks like the escaped characters inside emvTagData break the deserializer Commented Apr 26, 2019 at 15:10

3 Answers 3

2

Your emvTagData is a literal string but you are trying to deserialize it as a complex object.

"emvTagData":"{\"TVR\":\"0200008000\",\"PIN\":\"None\",\"Signature\":\"true\",\"Mode\":\"Issuer\",\"ARC\":\"Z3\",\"TSI\":\"E800\",\"Application Preferred Name\":\"CREDITO DE VISA\",\"AID\":\"A0000000031010\",\"IAD\":\"06010A03A00000\",\"Entry method\":\"Chip Read\",\"Application Label\":\"VISA CREDIT\"}"

Seeing that you've already got it as a string type commented out in your AuthCardResponse class I expect you've already found you could deserialize it as a string.

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

1 Comment

I was deserializing it in 2 steps where I would deserialize it to a string first and then deserialize it to an EmvTagData object and then assign it to my class object. Hmm, I was trying to get away from that.
1

You can use JSON to Csharp online to check your C# class

You need more step to get result by Deserialize emvTagData to your object.

http://json2csharp.com/

enter image description here With your JSON data, your class should be

public class RootObject
{
    public string amount { get; set; }
    public string resptext { get; set; }
    public string cvvresp { get; set; }
    public string respcode { get; set; }
    public string avsresp { get; set; }
    public string merchid { get; set; }
    public string token { get; set; }
    public string authcode { get; set; }
    public string respproc { get; set; }
    public string emvTagData { get; set; }
    public string retref { get; set; }
    public string respstat { get; set; }
    public string account { get; set; }
}

2 Comments

Visual Studio has had the ability to generate classes from JSON (or XML) for almost 10 years now
yes, but this link for whom don't open Visual Studio
0

Your json have bad syntax for Newtonsoft.Json. Your object (EmvTagData) in your json should look like this:

"emvTagData": {
"TVR": "0200008000",
"PIN": "None",
"Signature": "true",
"Mode": "Issuer",
"ARC": "Z3",
"TSI": "E800",
"Application Preferred Name": "CREDITO DE VISA",
"AID": "A0000000031010",
"IAD": "06010A03A00000",
"Entry method": "Chip Read",
"Application Label": "VISA CREDIT"}

Without \" and " on before and after main brackets.

Whole json :

{"amount":"3.00","resptext":"Approval","cvvresp":"P","respcode":"000","avsresp":"","merchid":"800000000800","token":"9478837814450119","authcode":"PPS306","respproc":"RPCT","emvTagData":{"TVR":"0200008000","PIN":"None","Signature":"true","Mode":"Issuer","ARC":"Z3","TSI":"E800","Application Preferred Name":"CREDITO DE VISA","AID":"A0000000031010","IAD":"06010A03A00000","Entry method":"Chip Read","Application Label":"VISA CREDIT"},"retref":"116390139157","respstat":"A","account":"47XXXXXXXXXX0119"}

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.