0

I am getting 'Error converting value' when I try to use deserilizeobject. My client sometimes sends data with quotes and special characters in it. It works when they try to serialize it. But it doesn't work when I try to deserilize it. I tried with escapehtml but still I have the same issue. It looks like 'SerializeObject' is not throwing error message that means it's valid JSON. Please let me know how to solve this issue.

string json2 = @"{
  'RootObject1':{
     't_date': '03-JAN-2016',
     't_summary': 'test """"""""""""'
   }
}";


var json3 = JsonConvert.SerializeObject(json2,  Newtonsoft.Json.Formatting.None,  new Newtonsoft.Json.JsonSerializerSettings{ StringEscapeHandling = Newtonsoft.Json.StringEscapeHandling.EscapeHtml });

var myJsonObject = JsonConvert.DeserializeObject<RootObject1>(json3);

class RootObject1
{
    public string t_date { get; set; }
    public string t_summary { get; set; }
}
5
  • Add RootObject class's code Commented Jul 18, 2017 at 15:30
  • Added RootObject code. Commented Jul 18, 2017 at 15:35
  • 1
    You're serializing a string into json3, so that's not going to deserialize to an object, only a string. Commented Jul 18, 2017 at 15:41
  • It works when I remove 'test """"""""""""' double quotes in the string. What is proper way to do this? Commented Jul 18, 2017 at 15:44
  • 1
    Correct me if I'm wrong, but json2 is just a string, if you want a string as a json object, you have to parse() it first, then you can use it as a json object, and deserialize() it into a .net object Commented Jul 18, 2017 at 16:07

1 Answer 1

1

This is not the correct way how you should use JsonConvert.Serialize and Deserialize.
At the beginning you should Serialize the object to string and then Deserialize back from string to object. Here is example of that:

RootObject1 ro = new RootObject1();
ro.t_date = "03-JAN-2016";
ro.t_summary = @"test """"""""""""";
var json3 = JsonConvert.SerializeObject(ro, typeof(RootObject1), Newtonsoft.Json.Formatting.None, new Newtonsoft.Json.JsonSerializerSettings { StringEscapeHandling = Newtonsoft.Json.StringEscapeHandling.EscapeHtml });
var myJsonObject = JsonConvert.DeserializeObject<RootObject1>(json3);
Console.WriteLine(myJsonObject.t_date + "\t" + myJsonObject.t_summary);

When you are trying to Serialize string then it will be Deserialze to string also. And it has no meaning at some point to do that.

Also if you want to get the object from JSON string you should do Deserealization and your JSON string is not valid. Here is example how you can achieve that:

string json2 = @"{
        't_date': '03-JAN-2016',
        't_summary': 'test """"""""""""'
    }";
var obj = JsonConvert.DeserializeObject<RootObject1>(json2);
Console.WriteLine(obj.t_date + "\t" + obj.t_summary);
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.