Newtonsoft Json反序列化为动态列表,boolean属性变为字符串

时间:2021-04-25 09:58:50

Can't seem to deserialize a dynamic list that contains a boolean property back into a boolean.
I have the following json.

似乎无法将包含布尔属性的动态列表反序列化为布尔值。我有以下json。

[
  {
    "Field1": 1,
    "Field2": "Test 1",
    "Field3": true
  },
  {
    "Field1": 2,
    "Field2": "Test 2",
    "Field3": false
  }  
]

When I use:

我用的时候:

Newtonsoft.Json.JsonConvert.DeserializeObject<List<dynamic>>(jsonString)

I get Field3 = "True" or "False"
When binding to a grid or other control, it thinks this is a "string" and not a "boolean".

我得到Field3 =“True”或“False”当绑定到网格或其他控件时,它认为这是一个“字符串”而不是“布尔”。

Any suggestions?

4 个解决方案

#1


4  

So I tried to install LinqPad and figure out why it was working for vendettamit yet it was not working in my C# application.
Which led me to this article on How to Dump a Newtonsoft JObject in LinqPad.

所以我尝试安装LinqPad并找出它为什么工作为vendettamit但它不能在我的C#应用​​程序中工作。这让我想到了如何在LinqPad中转储Newtonsoft JObject的这篇文章。

I then noticed that rdavisau used the following code.

然后我注意到rdavisau使用了以下代码。

JsonConvert.DeserializeObject<ExpandoObject>(jsonString)

Yet I was using the following code.

然而,我使用以下代码。

JsonConvert.DeserializeObject<List<dynamic>>(jsonString)

So once I changed my code to the following. It all worked correctly.

所以,一旦我将代码更改为以下内容。一切正常。

JsonConvert.DeserializeObject<List<ExpandoObject>>(jsonString)

ExpandoObject was the piece I was missing.

ExpandoObject是我失踪的那篇文章。

#2


2  

Since in JSON the value true is bool and "true" is string, it seems like a bug. I would create a new issue on their issue tracker for this.

因为在JSON中值true是bool而“true”是字符串,所以它看起来像一个bug。我会为他们的问题跟踪器创建一个新问题。

A workaround would be to create a strong typed model for it.

解决方法是为它创建一个强类型模型。

public class FieldData
{
    public int Field1 {get; set;}
    public string Field2 {get; set;}
    public bool Field3 {get; set;}
}

JsonConvert.DeserializeObject<List<FieldData>>(jsonString);

This has also the advantage of compiletime check and better runtime performance.

这还具有编译时检查和更好的运行时性能的优点。

#3


0  

I just tried replicating your code as is in LINQPAD with JSON.NET 9.0.1. I didn't see the issue there:

我只是尝试使用JSON.NET 9.0.1在LINQPAD中复制代码。我没有看到那里的问题:

Newtonsoft Json反序列化为动态列表,boolean属性变为字符串

#4


0  

I just ran:

我跑了:


string s = "[{ \"Field1\": 1, \"Field2\": \"Test 1\", \"Field3\": true }, { \"Field1\": 2, \"Field2\": \"Test 2\", \"Field3\": false }   ]";
var result = JsonConvert.DeserializeObject>(s);
Console.WriteLine("Type: {0}", result[0].Field3.Type);

and it gave me: Type: Boolean

它给了我:Type:Boolean

The problem is likely because the JValue is being coerced into a string during the bind.

问题可能是因为绑定期间JValue被强制转换为字符串。

#1


4  

So I tried to install LinqPad and figure out why it was working for vendettamit yet it was not working in my C# application.
Which led me to this article on How to Dump a Newtonsoft JObject in LinqPad.

所以我尝试安装LinqPad并找出它为什么工作为vendettamit但它不能在我的C#应用​​程序中工作。这让我想到了如何在LinqPad中转储Newtonsoft JObject的这篇文章。

I then noticed that rdavisau used the following code.

然后我注意到rdavisau使用了以下代码。

JsonConvert.DeserializeObject<ExpandoObject>(jsonString)

Yet I was using the following code.

然而,我使用以下代码。

JsonConvert.DeserializeObject<List<dynamic>>(jsonString)

So once I changed my code to the following. It all worked correctly.

所以,一旦我将代码更改为以下内容。一切正常。

JsonConvert.DeserializeObject<List<ExpandoObject>>(jsonString)

ExpandoObject was the piece I was missing.

ExpandoObject是我失踪的那篇文章。

#2


2  

Since in JSON the value true is bool and "true" is string, it seems like a bug. I would create a new issue on their issue tracker for this.

因为在JSON中值true是bool而“true”是字符串,所以它看起来像一个bug。我会为他们的问题跟踪器创建一个新问题。

A workaround would be to create a strong typed model for it.

解决方法是为它创建一个强类型模型。

public class FieldData
{
    public int Field1 {get; set;}
    public string Field2 {get; set;}
    public bool Field3 {get; set;}
}

JsonConvert.DeserializeObject<List<FieldData>>(jsonString);

This has also the advantage of compiletime check and better runtime performance.

这还具有编译时检查和更好的运行时性能的优点。

#3


0  

I just tried replicating your code as is in LINQPAD with JSON.NET 9.0.1. I didn't see the issue there:

我只是尝试使用JSON.NET 9.0.1在LINQPAD中复制代码。我没有看到那里的问题:

Newtonsoft Json反序列化为动态列表,boolean属性变为字符串

#4


0  

I just ran:

我跑了:


string s = "[{ \"Field1\": 1, \"Field2\": \"Test 1\", \"Field3\": true }, { \"Field1\": 2, \"Field2\": \"Test 2\", \"Field3\": false }   ]";
var result = JsonConvert.DeserializeObject>(s);
Console.WriteLine("Type: {0}", result[0].Field3.Type);

and it gave me: Type: Boolean

它给了我:Type:Boolean

The problem is likely because the JValue is being coerced into a string during the bind.

问题可能是因为绑定期间JValue被强制转换为字符串。