c# - C# - 从 JSON 反序列化,结果总是一个空对象
问题描述
我尝试从 JSON 字符串中读取 c# 中的对象 - 但结果始终是一个空对象,没有任何异常 - 但我没有看到错误
我从网络服务获得的 JSON 字符串是
{
"CustomUserFields":{
},
"CustomApplicationFields":{
},
"Attachments":[
],
"Tags":[
],
"HasModifyEntriesAccess":true,
"HasViewEntryContentsAccess":true,
"CommentPrompts":{
"AskForCommentOnViewPassword":false,
"AskForCommentOnViewOffline":false,
"AskForCommentOnModifyEntries":false,
"AskForCommentOnMoveEntries":false,
"AskForCommentOnMoveFolders":false,
"AskForCommentOnModifyFolders":false
},
"Id":"c51ca807-9e01-4652-95d0-645a0914b1ba",
"Name":"SecondOne",
"Username":"Second@test.domain",
"Password":null,
"Url":"",
"Notes":"Bla Bla Bla",
"GroupId":"1182570d-d22d-4f2a-babb-3dab4ff48852",
"Created":"2018-02-27T14:39:15+01:00",
"Modified":"2018-02-27T14:39:15+01:00",
"Expires":null,
"UsageComment":null
}
我的代码看起来像这样
DataContractJsonSerializer serF = new DataContractJsonSerializer(typeof(Credential));
Credential cred1 = new Credential();
MemoryStream msF = new MemoryStream(Encoding.UTF8.GetBytes(response2.Content));
cred1 = serF.ReadObject(msF) as Credential;
msF.Close();
[Serializable, XmlRoot("Credential"), DataContract(Name = "Credential")]
public class Credential
{
[DataMember]
public Guid id = Guid.Empty;
[DataMember]
public Guid groupid = Guid.Empty;
[DataMember]
public string name = String.Empty;
}
我从类中删除了一些属性以简化代码的阅读——但这没有区别
Credential 对象 cred1 始终具有空属性
解决方案
来自 Json 的 C# 类(通过:http://json2csharp.com/ 自动生成):
public class CustomUserFields
{
}
public class CustomApplicationFields
{
}
public class CommentPrompts
{
public bool AskForCommentOnViewPassword { get; set; }
public bool AskForCommentOnViewOffline { get; set; }
public bool AskForCommentOnModifyEntries { get; set; }
public bool AskForCommentOnMoveEntries { get; set; }
public bool AskForCommentOnMoveFolders { get; set; }
public bool AskForCommentOnModifyFolders { get; set; }
}
public class RootObject
{
public CustomUserFields CustomUserFields { get; set; }
public CustomApplicationFields CustomApplicationFields { get; set; }
public List<object> Attachments { get; set; }
public List<object> Tags { get; set; }
public bool HasModifyEntriesAccess { get; set; }
public bool HasViewEntryContentsAccess { get; set; }
public CommentPrompts CommentPrompts { get; set; }
public string Id { get; set; }
public string Name { get; set; }
public string Username { get; set; }
public object Password { get; set; }
public string Url { get; set; }
public string Notes { get; set; }
public string GroupId { get; set; }
public DateTime Created { get; set; }
public DateTime Modified { get; set; }
public object Expires { get; set; }
public object UsageComment { get; set; }
}
看一下区分大小写
推荐阅读
- javascript - 从node.js中的javascript函数获取mysql结果
- java - Java 中的 Pacman 碰撞
- symfony - 在 HTML QuickForm2 中单独呈现字段
- python - sqlalchemy 条目的烧瓶管理员奇怪的行为
- javascript - 在 NS 7 应用程序中使用 Nativescript Filterable ListPIcker 插件和 Vue.js
- java - 从 Swing 中的 MenuItem 打开一个对话框并在关闭之前验证该对话框
- .net - 不变字符串的日期值
- javascript - 有没有办法在 VScode 中的选定文本上运行自定义 js 代码?
- angular - Angular - 在http拦截器中自动传递组件名称作为参数
- python-3.x - TypeError:“浮动”和“同步”的实例之间不支持“>”