【问题标题】:PostAsJsonAsync Deep SerializationPostAsJsonAsync 深度序列化
【发布时间】:2015-08-19 17:13:46
【问题描述】:

我正在使用 PostAsJsonAsync 将参数传递给 Web api 服务,就像这样

public T Save<T>(string url, T item) where T : class
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = _dbServiceUriProvider.GetUri();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            var response = client.PostAsJsonAsync(url, item).Result;
            if (!response.IsSuccessStatusCode)
            {
                throw new DbServiceException("Error in DbServiceDataSaver:\r\n" + response.ReasonPhrase);
            }
            T returnVar = response.Content.ReadAsAsync<T>().Result;
            return returnVar;
        }
    }

问题是这只是序列化我传递的对象,而不是任何子对象。例如,如果我有一个具有 HashSet 成员的类 Foo ,则 Bars 不会被序列化。这是因为 HashSets 无法序列化还是这里发生了其他事情?

【问题讨论】:

  • HashSet&lt;T&gt; 可以序列化。那不是问题。您的T item 是什么样的,您收到的输出 JSON 是什么?
  • 如何查看JSON,是否需要使用Fiddler之类的?
  • 有可能,是的。

标签: c# serialization asp.net-web-api2


【解决方案1】:

在类级别使用 Serializable 属性,在属性级别使用数据成员,它将为您工作或使用 json.serializeObjectmethod。 并且查看 json 数据不需要 fiddler 只需安装一个名为 json viewer 的插件,它将在浏览器本身上以 json 格式显示您的数据。 [https://addons.mozilla.org/en-Us/firefox/addon/jsonview/][1]

【讨论】:

    【解决方案2】:

    事实证明,我的类中只有一些属性具有 [DataMember] 属性,因此只有这些属性被序列化

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-06
      • 1970-01-01
      • 2023-03-21
      • 2013-04-12
      • 2011-01-23
      • 1970-01-01
      • 2014-03-14
      相关资源
      最近更新 更多