C#-从JSON反序列化,结果始终为空对象
我尝试从JSON字符串中读取c#中的对象,但结果总是空对象,没有任何异常,但我没有看到错误 我从Web服务获得的JSON字符串是C#-从JSON反序列化,结果始终为空对象,c#,json,deserialization,C#,Json,Deserialization,我尝试从JSON字符串中读取c#中的对象,但结果总是空对象,没有任何异常,但我没有看到错误 我从Web服务获得的JSON字符串是 { "CustomUserFields":{ }, "CustomApplicationFields":{ }, "Attachments":[ ], "Tags":[ ], "HasModifyEntriesAccess":true, "HasViewEntryContentsA
{
"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;
}
我从类中删除了一些属性以简化代码的读取-但这没有什么区别
凭证对象cred1始终具有Json中的空属性C#类(通过以下方式自动生成)
:
公共类CustomUserFields
{
}
公共类CustomApplicationFields
{
}
公共类注释提示
{
公共bool AskForCommentOnViewPassword{get;set;}
公共bool AskForCommentOnViewOffline{get;set;}
公共bool askforcommentomodifyentries{get;set;}
公共bool AskForCommentOnMoveEntries{get;set;}
public bool AskForCommentOnMoveFolders{get;set;}
public bool askforcommentomodifyfolders{get;set;}
}
公共类根对象
{
公共CustomUserFields CustomUserFields{get;set;}
公共CustomApplicationFields CustomApplicationFields{get;set;}
公共列表附件{get;set;}
公共列表标记{get;set;}
public bool HasModifyEntriesAccess{get;set;}
public bool HasViewEntryContentsAccess{get;set;}
公共评论提示{get;set;}
公共字符串Id{get;set;}
公共字符串名称{get;set;}
公共字符串用户名{get;set;}
公共对象密码{get;set;}
公共字符串Url{get;set;}
公共字符串注释{get;set;}
公共字符串GroupId{get;set;}
已创建公共日期时间{get;set;}
修改的公共日期时间{get;set;}
公共对象过期{get;set;}
公共对象UsageComment{get;set;}
}
查看区分大小写现在不确定,反序列化是否区分大小写?Bcs。您使用了小写的属性&JSON作为CamelCase。您使用了
response2
,这不可能从您的代码中看到定义,但这不应该是一个请求对象吗?还可以使用使用
流块来确保所有内容都被关闭/处置,这样,如果您不小心,就不会导致内存泄漏
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; }
}