转换JSON时出错



当我试图调用时

  `var obj = JsonConvert.DeserializeObject<UserModel>({myjsonString})`

它一直让我无法反序列化异常。为了检查我的json字符串是否格式正确,我决定解析字符串并调用

JsonSchema schema = JsonSchema.Parse({myjsonString});

现在我得到了下面的错误,不太确定是什么意思

附加信息:分析架构对象时需要对象,得到了字符串。路径"

**UPDATE**
"{"Id":5,"Username":"Sid","FirstName":"Sid ","LastName":"LastSid","Email":"test@gmail.com","Password":"sample","GravatarHash":"http://www.gravatar.com/avatar/f4f901415af5aff35801e8444cd5adc1?d=retro&?s=50","Country":"Moon","OrganizationId":1,"IsLocked":false,"CreatedDate":"12/13/2013 2:34:28 AM","UpdatedDate":"12/13/2013 2:34:28 AM","DataLoaded":true}"   

更新2

""{\"Id\":5,\"Username\":\"Sid\",\"FirstName\":\"Siddharth \",\"LastName\":\"Kosta\",\"Email\":\"Skosta@gmail.com\",\"Password\":\"PAssword\",\"GravatarHash\":\"http://www.gravatar.com/avatar/f4f901415af5aff35801e8c4bcd5adc1?d=retro&?s=50\",\"Country\":\"India\",\"OrganizationId\":1,\"IsLocked\":false,\"CreatedDate\":\"2013-12-13T02:34:28.037\",\"UpdatedDate\":\"2013-12-13T02:34:28.23\",\"DataLoaded\":true}""

用户模型

  public class UserModel
    {
        public Int32 Id { get; set; }
        public String Username { get; set; }
        public String FirstName { get; set; }
        public String LastName { get; set; }
        public String Email { get; set; }
        public String Password { get; set; }
        public String GravatarHash { get; set; }
        public String Country { get; set; }
        public Int32 OrganizationId { get; set; }
        public Boolean IsLocked { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime UpdatedDate { get; set; }
    } 

我也试过

public String CreatedDate { get; set; }
public String UpdatedDate { get; set; }

思考日期是否导致问题

更新:

它与你的UserModel完美配合,至少对我来说是这样

假设您有这样的UserModel:

public class UserModel
{
    public int Id { get; set; }
    public string Username { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string GravatarHash { get; set; }
    public string Country { get; set; }
    public int OrganizationId { get; set; }
    public bool IsLocked { get; set; }
    public DateTime CreatedDate { get; set; }
    public DateTime UpdatedDate { get; set; }
    public bool DataLoaded { get; set; }
}
var input =
    "{"Id":5,"Username":"Sid","FirstName":"Sid ","LastName":"LastSid","Email":"test@gmail.com","Password":"sample","GravatarHash":"http://www.gravatar.com/avatar/f4f901415af5aff35801e8444cd5adc1?d=retro&?s=50","Country":"Moon","OrganizationId":1,"IsLocked":false,"CreatedDate":"12/13/2013 2:34:28 AM","UpdatedDate":"12/13/2013 2:34:28 AM","DataLoaded":true}";
var userModel = JsonConvert.DeserializeObject<UserModel>(input);

我认为你的模型有问题,你能提供一下吗?

在我看来,您的JSON正在进行双重序列化。(JSON中有一堆额外的反斜杠就是这种情况的症状。)我注意到在另一个答案的评论中,你说你正在使用Web API。Web API框架为您处理序列化,因此您不需要在这些方法中调用JsonConvert.SerializeObject()。相反,直接返回结果即可。然后,您应该能够在客户端中正常地反序列化它。请参阅此问题。

中使用大括号有原因吗

var obj = JsonConvert.DeserializeObject<UserModel>({myjsonString})

这似乎是错误的根源。更改为:

var obj = JsonConvert.DeserializeObject<UserModel>(myjsonString)

您缺少DataLoaded属性。

public bool DataLoaded { get; set; }

将来,使用此网站从JSON生成C#类。

http://json2csharp.com/


编辑:

一步一步地尝试。。。按原样复制并粘贴此类。

public class UserModel
{
    public int Id { get; set; }
    public string Username { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string GravatarHash { get; set; }
    public string Country { get; set; }
    public int OrganizationId { get; set; }
    public bool IsLocked { get; set; }
    public string CreatedDate { get; set; }
    public string UpdatedDate { get; set; }
    public bool DataLoaded { get; set; }
}

现在在控制台中有这个:

    var jsonString = @"{""Id"":5,""Username"":""Sid"",""FirstName"":""Sid "",""LastName"":""LastSid"",""Email"":""test@gmail.com"",""Password"":""sample"",""GravatarHash"":""http://www.gravatar.com/avatar/f4f901415af5aff35801e8444cd5adc1?d=retro&?s=50"",""Country"":""Moon"",""OrganizationId"":1,""IsLocked"":false,""CreatedDate"":""12/13/2013 2:34:28 AM"",""UpdatedDate"":""12/13/2013 2:34:28 AM"",""DataLoaded"":true}";
    var user = JsonConvert.DeserializeObject<UserModel>(jsonString);
    Console.WriteLine(user.Country);
    Console.ReadLine();

相关内容

  • 没有找到相关文章

最新更新