简体   繁体   English

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

[英]C# - deserialization from JSON, result is always an empty object

I try to read an object in c# from a JSON string - but the result is always an empty object without any exception - but I don't see an error 我尝试从JSON字符串中读取c#中的对象 - 但结果始终是一个空对象,没有任何异常 - 但我没有看到错误

The JSON string that I get from a webservice is 我从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
}

My code looks like this 我的代码看起来像这样

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;
}

I removed some properties from the class to simplfy reading the code - but that makes no difference 我从类中删除了一些属性以简化读取代码 - 但这没有任何区别

The Credential object cred1 has always empty attributes 凭据对象cred1始终为空属性

C# Classes from your Json (Auto generated via: http://json2csharp.com/ ) : 来自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; }
}

Take a look to case sensitive 看一下区分大小写

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM