ServiceStack反序列化不构建对象

时间:2014-06-24 10:27:54

标签: c# servicestack json-deserialization

我正在使用servicestack将从Web服务获得的JSON反序列化为对象。 该过程有效(没有例外),但我无法访问反序列化对象中的类。

我的代码称之为:

  LoginResultModel result = new LoginResultModel
            {
                Avatars = new Avatars(),
                Country = new Country(),
                RootObject = new RootObject()
            };


    result = client.Post<LoginResultModel>(URL, postData);

一旦我回到结果(类型为LoginResultModel),我就无法访问其中的任何内容! Intellisense不会帮助 - &#34;结果。&#34;不要显示与该课程相关的任何内容。

我猜测我对层次结构做错了什么? (很奇怪,因为没有抛出异常)。我做错了什么?

反序列化形式的JSON(使用json2Csharp):

 public class LoginResultModel
{
    /// <summary>
    /// IsLogedIn method 
    /// </summary>

    public Country Country { get; set; }
    public Avatars Avatars { get; set; }
    public RootObject RootObject { get; set; }

}

public class Country
{
    public string Name { get; set; }
    public string A2 { get; set; }
    public int Code { get; set; }
    public int PhonePrefix { get; set; }
}

public class Avatars
{
    public string Small { get; set; }
    public string Medium { get; set; }
    public string Large { get; set; }
}

public class RootObject
{
    public int CID { get; set; }
    public string Username { get; set; }
    public Country Country { get; set; }
    public string URL { get; set; }
    public int AffiliateID { get; set; }
    public Avatars Avatars { get; set; }
    public bool IsLoggedIn { get; set; }
    public bool AllowCommunity { get; set; }
    public string Token { get; set; }
    public int TokenExpirationInMinutes { get; set; }
    public bool IsKYCRequired { get; set; }
}

1 个答案:

答案 0 :(得分:3)

您的LoginResultModel课程不包含任何公共属性。所以没有什么可以序列化,然后你会得到一个空的结果。

您所做的是创建LoginResultModel中的其他类,我相信您打算将其作为属性实现。

你应该做的是创建这样的类:

public class Country
{
    public string Name { get; set; }
    public string A2 { get; set; }
    public int Code { get; set; }
    public int PhonePrefix { get; set; }
}

public class Avatars
{
    public string Small { get; set; }
    public string Medium { get; set; }
    public string Large { get; set; }
}

public class RootObject
{
    public int CID { get; set; }
    public string Username { get; set; }
    public Country Country { get; set; }
    public string URL { get; set; }
    public int AffiliateID { get; set; }
    public Avatars Avatars { get; set; }
    public bool IsLoggedIn { get; set; }
    public bool AllowCommunity { get; set; }
    public string Token { get; set; }
    public int TokenExpirationInMinutes { get; set; }
    public bool IsKYCRequired { get; set; }
}

LoginResultModel具有其他类的类型的属性:

public class LoginResultModel
{
    public Country Country { get; set; }
    public Avatars Avatars { get; set; }
    public RootObject RootObject { get; set; }
}

然后在您的操作方法中,您需要使用这些对象的实例填充LoginResultModel

public class MyLoginService : Service
{
    public LoginResultModel Post(LoginRequest request)
    {
        // Your login logic here
        return new LoginResultModel {
            Country = new Country { Name = "United States", A2 = "Something", Code = 1, PhonePrefix = 555},
            Avatars = new Avatars { Small = "small.png", Medium = "medium.png", Large = "large.png" },
            RootObject = new RootObject { 
                CID = 123,
                Username = "",
                ...
            }
        };
    }
}

希望有所帮助。