C# Newtonsoft JSON在列表中反序列化布尔值
我使用Newtonsoft Json()库将一些Json反序列化为对象,但在布尔值方面遇到了一些问题。请看我下面的例子。只要您引用Newtonsoft dll(目前我使用的是最新版本,文件版本为6.0.3.17227),该示例就应该在LinqPad中运行。该问题正在反序列化到UpdateLocationRequest对象中 感谢您的帮助C# Newtonsoft JSON在列表中反序列化布尔值,c#,json,json.net,C#,Json,Json.net,我使用Newtonsoft Json()库将一些Json反序列化为对象,但在布尔值方面遇到了一些问题。请看我下面的例子。只要您引用Newtonsoft dll(目前我使用的是最新版本,文件版本为6.0.3.17227),该示例就应该在LinqPad中运行。该问题正在反序列化到UpdateLocationRequest对象中 感谢您的帮助 void Main() { string json1 = "{\"token\":\"5b2a38c8-c211-481e-aa75-7d52fff6e
void Main()
{
string json1 = "{\"token\":\"5b2a38c8-c211-481e-aa75-7d52fff6eb2f\",\"share\":true}";
string json2 = "{\"token\":\"5b2a38c8-c211-481e-aa75-7d52fff6eb2f\",\"locationList\":[{\"desc\":\"This is a test\",\"name\":\"Andrew 3\",\"deviceLocationId\":\"a8d2bfae-4493-41cd-ae1e-ea0da66da0cf\",\"locType\":1,\"lon\":-80.27543,\"lat\":43.42618,\"share\":true}]}";
TestClass req1 = JsonConvert.DeserializeObject<TestClass>(json1);
UpdateLocationsRequest req2 = JsonConvert.DeserializeObject<UpdateLocationsRequest>(json2);
json1.Dump("json1");
req1.Dump("Boolean ok here");
json2.Dump("json2");
req2.Dump("Boolean not ok here. Why not?");
}
// Define other methods and classes here
public class UpdateLocationsRequest
{
public string token { get; set; }
public List<LocationJson> locationList { get; set; }
}
public class LocationJson
{
public string deviceLocationId { get; set; }
public string name { get; set; }
public string desc { get; set; }
public int locType { get; set; }
public float lat { get; set; }
public float lon { get; set; }
public bool show { get; set; }
}
public class TestClass {
public string token {get; set;}
public bool share {get; set;}
}
void Main()
{
字符串json1=“{\'token\”:\“5b2a38c8-c211-481e-aa75-7d52fff6eb2f\”,\“share\”:true}”;
字符串json2=“{\'token\”:\“5b2a38c8-c211-481e-aa75-7d52fff6eb2f\”,“位置列表”:[{\'desc\”:“这是一个测试”,“名称\”:“Andrew 3\”,“deviceLocationId\”:“a8d2bfae-4493-41cd-ae1e-ea0da66da0cf\”,“位置类型”:1,“lon\”:-80.27543,“:LAT428”;“真实共享];
TestClass req1=JsonConvert.DeserializeObject(json1);
UpdateLocationRequestReq2=JsonConvert.DeserializeObject(json2);
json1.转储(“json1”);
请求1.转储(“此处为布尔ok”);
json2.Dump(“json2”);
请求2.转储(“布尔值不正常,为什么不?”);
}
//在此处定义其他方法和类
公共类UpdateLocationRequest
{
公共字符串标记{get;set;}
公共列表位置列表{get;set;}
}
公共类位置JSON
{
公共字符串deviceLocationId{get;set;}
公共字符串名称{get;set;}
公共字符串desc{get;set;}
public int locType{get;set;}
公共浮点lat{get;set;}
公共浮点lon{get;set;}
公共bool show{get;set;}
}
公共类TestClass{
公共字符串标记{get;set;}
公共布尔共享{get;set;}
}
您的json bool值是share
,但在您的类中是show
。调整一个或另一个,使其匹配,然后您就可以开始了我发现了您的问题。LocationJson类
有一个名为show
的布尔属性,而json2字符串有一个属性share
。节目从不更新。所有其他值都将更新
添加断点并单步执行程序并查看正在发生的事情总是一件好事
祝你好运 哇,谢谢。我觉得自己像个傻瓜。我有一个对象与“显示”位置相关,另一个对象与“共享”联系信息相关。我把财产的名字划掉了。我想,我只是需要第二次观察代码。再次感谢!