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

时间:2018-05-28 08:29:18

标签: c# json deserialization

我尝试从JSON字符串中读取c#中的对象 - 但结果总是一个没有任何异常的空对象 - 但我没有看到错误

我从网络服务获得的JSON字符串是

class CFileRow : public CObject
   {
//8 fields
public:
    CFileRow(const string line)
      {
      //convert string line that you are to read from file into class
      }
    ~CFileRow(){}
   };
CArrayObj* fileRowArray = new CArrayObj();

while(!FileIsEnding(FileReader))
  {
     string line=FileReadString(FileReader);
     fileRowArray.Add(new CFileRow(line));
  }

我的代码看起来像这样

{  
   "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
}

我从类中删除了一些属性来简单地阅读代码 - 但这并没有什么区别

凭据对象cred1始终为空属性

1 个答案:

答案 0 :(得分:0)

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

看一下区分大小写的