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
The JSON string that I get from a webservice is
{
"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
C# Classes from your Json (Auto generated via: 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.