【问题标题】:ServiceStack Deserialization not building an objectServiceStack反序列化不构建对象
【发布时间】:2014-06-24 10:27:54
【问题描述】:

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

我的代码这样称呼:

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


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

一旦我得到结果(LoginResultModel 类型),我就无法访问其中的任何内容! Intellisense 无济于事——“结果”。不要显示与该类相关的任何内容。

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

反序列化形式的 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; }
}

【问题讨论】:

    标签: c# servicestack json-deserialization


    【解决方案1】:

    您的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 = "",
                    ...
                }
            };
        }
    }
    

    希望对您有所帮助。

    【讨论】:

    • 感谢您的帮助,我有点明白您的意思,但是在编辑代码后(编辑我的帖子以反映新代码的样子)我现在收到如下错误:类型定义应该以一个'{',期望序列化类型'LoginResultModel',得到的字符串以: 开头
    • @Yosi199 现在您已经编辑了问题,我可以更好地了解您要做什么。我现在意识到您正在调用第 3 方 api 而不是您自己的 ServiceStack 服务。您的问题是第 3 方响应没有返回 ServiceStack 客户端期望的 JSON。它正在返回 HTML,其中可能包含一些错误消息。您只能通过确保您发出的请求是正确的并被第 3 方 api 理解来解决此问题,以便它返回 JSON。
    • 你是对的 - 我发送了一个错误的请求。我没有收到 JSON,因为在我的请求中我没有添加标题。我想我错误地使用了服务堆栈 JsonServiceClient.post - 我需要发送带有标头(内容类型)的请求。我应该求助于使用 WebRequest 吗?还是一种使用服务堆栈发送请求和标头的方法?
    • @Yosi199 JsonServiceClient 会自动将 ContentType 设置为 application/json 并且您的请求 client.Post&lt;LoginResultModel&gt;(URL, postData) 看起来确实正确。但是,如果 postData 不是服务所期望的,它可能会忽略 contentType 以提供错误。由于没有 REST 标准,因此很难知道第 3 方 API。我怀疑您调用的 API 在出现问题时会忽略标头。最好阅读他们的 api 文档,了解他们如何处理错误。 Chrome 的 Postman 扩展是测试 API 响应的好方法。
    • @Yosi199 在这种情况下,您最好使用标准的WebRequest 帖子,因为JsonServiceClient 会将请求序列化为JSON,而不是API 所需的URLencoding 格式,这就是它不会的原因不喜欢回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    相关资源
    最近更新 更多