Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#反序列化具有不同属性名称的JSON字符串_C#_Json_Json.net_Deserialization - Fatal编程技术网

C#反序列化具有不同属性名称的JSON字符串

C#反序列化具有不同属性名称的JSON字符串,c#,json,json.net,deserialization,C#,Json,Json.net,Deserialization,我很难将C#中的JSON字符串反序列化为属性名称与JSON字符串中的属性名称略有不同的对象。这是我的密码: 使用System.Collections.Generic; 使用Newtonsoft.Json; [JsonObject(MemberSerialization.OptIn)] 公共类拍卖文件列表 { [JsonProperty(“文件”)] 公共列表文件{get;set;} } [JsonObject(MemberSerialization.OptIn)] 公共类拍卖文件 { #区域属性

我很难将C#中的JSON字符串反序列化为属性名称与JSON字符串中的属性名称略有不同的对象。这是我的密码:

使用System.Collections.Generic;
使用Newtonsoft.Json;
[JsonObject(MemberSerialization.OptIn)]
公共类拍卖文件列表
{
[JsonProperty(“文件”)]
公共列表文件{get;set;}
}
[JsonObject(MemberSerialization.OptIn)]
公共类拍卖文件
{
#区域属性
[JsonProperty(“url”)]
公共字符串Url{get;set;}
[JsonProperty(“lastModified”)]
公共长LastModified{get;set;}
#端区
}
字符串jsonResponse=“{\'文件\”:[{\'url\':\”http://auction-
api.com/auctions.json\“,\“lastModified\”:1495397839000}]}”
AuctionFilesList auctionFiles=JsonConvert.DeserializeObject(jsonResponse);
我没有收到任何错误,只是无法反序列化到对象中。我已尝试按如下方式添加序列化程序设置,但仍然失败:

JsonSerializerSettings jss=new JsonSerializerSettings{ContractResolver=new CamelCasePropertyNamesContractResolver()};
AuctionFilesList auctionFiles=JsonConvert.DeserializeObject(jsonResponse,jss);
我的类结构与json2csharp返回的内容一致,当我将对象的属性名称与JSON匹配时,它就会工作,因此我要么误解了JsonProperty的作用,要么不知何故被忽略了


非常感谢您的帮助。

您的代码可以正常工作,无需对其进行任何修改。是否确实未反序列化对象?反序列化后,auctionFiles的Files属性设置为null。该对象已正确反序列化,您可以访问
auctionFiles.Files[0]。Url
和get
http://auction-api.com/auctions.json
基于@Federico Dipuma的小提琴。奇怪。当我在visual studio中运行时,效果不一样。您的代码在没有任何修改的情况下工作。是否确实未反序列化对象?反序列化后,auctionFiles的Files属性设置为null。该对象已正确反序列化,您可以访问
auctionFiles.Files[0]。Url
和get
http://auction-api.com/auctions.json
基于@Federico Dipuma的小提琴。奇怪。当我在VisualStudio中运行时,效果不一样。