Json.Net boolean parsing issue

JObject.Parse (jsonString) causes a problem for boolean data. e.g. Json:

{ "BoolParam": true } 

I used the following code for parsing:

 JObject data = JObject.Parse(str1); foreach (var x in data) { string name = x.Key; Console.Write(name + " ("); JToken value = x.Value; Console.Write(value.Type + ")\n"); Console.WriteLine(value); } 

This value is output as: BoolParam (Boolean): True

Case sensitivity is causing a problem as I save this json for later use. Saved json looks like

 { "BoolParam": true } 

However, when I use it later, JObject.Parse (str) throws an error as invalid Json: Unexpected character encountered while parsing the value: T. Path 'BoolParam', line 2, position 15.

If I changed the case from "True" to "true", it works. I do not want to add this hack to change the case when saving, but is there a better way to handle this scenario.

+7
source share
1 answer

I do not want to add this hack to change case when saving, but there is a better way to handle this scenario.

No, you need to create a valid JSON on save if you want to deserialize it later using a JSON serializer like Newtonsoft JSON. Thus, fixing the save route is the right way.

+5
source share

All Articles