Json.Net boolean parsing issue

旧巷老猫 提交于 2020-01-03 07:59:07

问题


JObject.Parse(jsonString) is causing issue for boolean data. e.g. The json is :

{
    "BoolParam": true
}

I used the following code to parse:

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 print out the value as : BoolParam (Boolean) : True

The case sensitivity causes issue as I save this json for later use. The saved json looks like

{
    "BoolParam": True
}

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

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


回答1:


I dont want to add this hack to change the case when saving but is there a better way to handle this scenario.

No, you have to produce valid JSON when saving if you want to be able to later deserialize it with a JSON serializer such as Newtonsoft JSON. So fixing your saving routing is the right way to go here.



来源:https://stackoverflow.com/questions/19010892/json-net-boolean-parsing-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!