0

I am receiving the following Json string as output from a REST service:

"CategoryDtls":       
             {
            "category": "S",
            "msgType": "0",
            "sourceId": "21999"
           }

I wish to convert it to a C# object using Json.net

public partial class CategoryDtls
    {
        private TestChar categoryField;
        public TestChar category
        {
            get
            {
                return categoryField;
            }

            set
            {
                if (value == null)
                {
                    categoryField = null;
                }

                var stringValue = Convert.ToString(value);
                var charClass = new TestChar();
                charClass.SimpleChar = stringValue;
                categoryField = charClass;

            }
        }
        public string msgType { get; set; }
        public string sourceId { get; set; }
    }

public partial class TestChar : object, System.ComponentModel.INotifyPropertyChanged
    {

        private string simpleCharField;

        public string SimpleChar
        {
            get
            {
                return this.simpleCharField;
            }
            set
            {
                this.simpleCharField = value;
                this.RaisePropertyChanged("SimpleChar");
            }
        }

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null))
            {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }

When doing so, I get an error like

{"Could not cast or convert from System.String to JsonParser.TestChar."}

I tried to customize the set method for Category property like above but couldn't resolve it. I can't change the Json string. Is there anyway, I can resolve this issue.

1 Answer 1

1

You could create a custom CategoryConverter and use it by declaring it [JsonConverter(typeof(CategoryConverter))] above your public TestChar category.

public class CategoryConverter : JsonConverter
{
    // Declared as abstract in JsonConverter so must be overridden
    public override bool CanConvert(Type objectType) { return true; }

    // Declared as abstract in JsonConverter so must be overridden
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        return JToken.Load(reader).ToObject<TestChar>();
    }
}
Sign up to request clarification or add additional context in comments.

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.