Json.Net布尔解析问题 [英] Json.Net boolean parsing issue

查看:112
本文介绍了Json.Net布尔解析问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JObject.Parse(jsonString)引起布尔数据问题.例如json是:

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);
}

这将值打印为: BoolParam(布尔值):

This print out the value as : BoolParam (Boolean) : True

区分大小写会导致问题,因为我保存了此json供以后使用.保存的json看起来像

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

{
    "BoolParam": True
}

但是,当我以后使用它时,JObject.Parse(str)抛出错误,因为无效的Json:解析值时遇到了意外字符:T.路径'BoolParam',第2行,位置15.

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.

如果我将大小写从"True"更改为"true",则可以使用.我不想添加此技巧来更改保存时的大小写,但是有一种更好的方法来处理这种情况.

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.

推荐答案

我不想添加此技巧来更改保存时的大小写,但是 有更好的方法来处理这种情况.

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

否,如果希望以后可以使用JSON序列化程序(例如Newtonsoft JSON)反序列化,则保存时必须产生有效的JSON.因此,修正保存的路由是正确的方法.

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.

这篇关于Json.Net布尔解析问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆