【问题标题】:Deserializing Json object gets a "null field"反序列化 Json 对象获得“空字段”
【发布时间】:2016-01-31 10:38:11
【问题描述】:

我一直在努力弄清楚为什么在反序列化 json 对象时会得到一个空字段

public OAuthResponse Get(OAuthRequest TEntity)
    {
     OAuthResponse Oauthresponse = new OAuthResponse();
        using (System.Net.Http.HttpClient c = GetHttpClient())
        {
            string SerializedEntity = JsonConvert.SerializeObject(TEntity);
            HttpContent content = new StringContent(SerializedEntity);
            content.Headers.ContentType = new MediaTypeWithQualityHeaderValue(MessageType);

            HttpResponseMessage Response = c.PostAsync(_Path, content).Result;

            string Json = Response.Content.ReadAsStringAsync().Result;

            Oauthresponse = JsonConvert.DeserializeObject<OAuthResponse>(Json);
        }
}

到达线路时:

string Json = Response.Content.ReadAsStringAsync().Result;

悬停到“Json”,一切似乎都很好,因为没有字段为空

下一行的反序列化将反序列化的 json 对象分配给 OAuthResponse 对象,但不知何故将一个字段保留为空。

是编码问题吗?在 API 文档中,它说编码必须设置为 System.Text.Encoding.Default。

添加类似

的行
content.Headers.ContentEncoding.Clear();
content.Headers.ContentEncoding.Add("Default"); // "Default" or "utf-8"

不要帮忙。

我的类中所有字段的名称都与 json 对象的键匹配,并且它们的类型相同。

我很确定这是一个简单的问题,但我被困在这一点上。

非常感谢!

【问题讨论】:

    标签: c# json serialization deserialization


    【解决方案1】:

    也许这条线

    string Json = Response.Content.ReadAsStringAsync().Result;
    

    错了。使用:

    string Json = await Response.Content.ReadAsStringAsync();
    

    相反。使用异步函数进行调试可能会很奇怪。

    [编辑]

    我不知道OAuthResponse这个类,所以我认为它是这样的:

    public class OAuthResponse
    {
       public string access_token;
       public string token_type;
       public int expires_in;
       public int created_at;
       public string refresh_token;
       public string scope;
    }
    

    那么这三行就起作用了:

        string Json = "{\"access_token\":\"18312b7c108b6084e1e48afjklem51c357733ba1751d1c746e2698304b0‌​83cd6\",\"token_type\":\"bearer\",\"expires_in\":7776000,\"refresh_token\":\"995b‌​04af1d408240egkt85ee560a5571f503cecee294fd241abef3e1b8deda9df5\",\"scope\":\"publ‌​ic\",\"created_at\":1454239878}";
        JavaScriptSerializer ser = new JavaScriptSerializer();
        OAuthResponse Oauthresponse = ser.Deserialize<OAuthResponse>(Json);
    

    【讨论】:

    • 嘿,谢谢您的回答。如果我这样做,我必须将我的方法更改为公共异步,在这种情况下,Visual Studio 告诉我返回类型必须是“void”或“Task”
    • 这是正确的。或者您使用该函数的非异步版本。如果存在。
    • 你的返回类型是Task&lt;OAuthResponse&gt;
    • 这变得越来越复杂,我正在更改我的 Iservice 接口、我的服务定位器和我的 Oauth 服务类。我会去找它并告诉你进展如何,谢谢
    • 好的,我做了所有这些,但我仍然有同样的错误......另一个想法?非常感谢您的宝贵时间!
    【解决方案2】:

    在我的 OAuthResponse 中的每个字段上添加具有相应名称的 [JsonProperty(PropertyName = "JsonObjectPropertyName")] 并更改我的字段名称就可以了!

    在这种情况下:

    /// <summary>The OAuth access token</summary>
        [JsonProperty(PropertyName = "access_token")]
        public string AccessToken;
    
        /// <summary>The type of the OAuth token</summary>
        [JsonProperty(PropertyName = "token_type")]
        public string TokenType;
    
        /// <summary>The date when the OAuth token will expire</summary>
        [JsonProperty(PropertyName = "expires_in")]
        public int ExpiresIn;
    
        /// <summary>The OAuth refresh token</summary>
        [JsonProperty(PropertyName = "refresh_token")]
        public string RefreshToken;
    
        /// <summary>The scope of the OAuth token</summary>
        [JsonProperty(PropertyName = "scope")]
        public string Scope;
    

    谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多