C# - 从JSON反序列化,结果始终为空对象

3

我想从一个JSON字符串中读取C#对象,但结果总是一个空对象,没有任何异常,但我看不到错误。

我从Web服务得到的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的属性始终为空。


4
现在不确定,反序列化时是否区分大小写?因为属性是小写的,而JSON是驼峰式命名。 - Tatranskymedved
你使用了 response2,但是从你的代码中无法看到它的定义,但这不应该是一个请求对象吗? - freefaller
还可以使用using块来处理流,以确保所有内容都会被关闭/处理,这样就不会因为疏忽而引入内存泄漏。 - Jordy van Eijk
1个回答

0

C#类从您的Json中生成(通过以下方式自动生成: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; }
}

注意大小写


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接