解析JSON时出错

时间:2013-12-13 12:10:12

标签: c# json.net

我正在尝试拨打

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

考虑日期是否导致问题

4 个答案:

答案 0 :(得分:2)

更新

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

我认为您的型号有问题,请您提供吗?

答案 1 :(得分:1)

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

答案 2 :(得分:0)

是否有理由在

中使用大括号
var obj = JsonConvert.DeserializeObject<UserModel>({myjsonString})

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

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

答案 3 :(得分:0)

您缺少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();