ServiceStack.Text and DeserializeFromString

本文关键字:DeserializeFromString and Text ServiceStack | 更新日期: 2023-09-27 18:23:38

我承认我可能忘记了一些非常简单的事情,但我不知道是什么

我有一门课:

public class UserAgentInfo
{
    public string agent_type { get; set; }
    public string agent_name { get; set; }
    public string agent_version { get; set; }
    public string os_type { get; set; }
    public string os_name { get; set; }
    public string os_versionName { get; set; }
    public string os_versionNumber { get; set; }
    public string linux_distibution { get; set; }
}

然后我从Url中获取Json字符串,并尝试将其反序列化为:

using (System.Net.WebClient wc = new System.Net.WebClient())
{
    string json = wc.DownloadString("http://www.useragentstring.com/?getJSON=agent_type-agent_name-agent_version-os_type-os_name-os_versionName-os_versionNumber-linux_distibution&uas=" + o.Browser);
    agentInfo = ServiceStack.Text.TypeSerializer.DeserializeFromString<UserAgentInfo>(json);
}

json字符串为:

"{'"agent_type'":'"Browser'",'"agent_name'":'"Chrome'",'"agent_version'":'"28.0.1500.72'",'"os_type'":'"Windows'",'"os_name'":'"Windows 7'",'"os_versionName'":'"'",'"os_versionNumber'":'"'",'"linux_distibution'":'"Null'"}"

或者对于那些不习惯C#的

"{"agent_type":"Browser","agent_name":"Chrome","agent_version":"28.0.1500.72","os_type":"Windows","os_name":"Windows 7","os_versionName":"","os_versionNumber":"","linux_distibution":"Null"}"

返回的对象为:

{...UserAgentInfo}
   agent_name: null
   agent_type: null
   agent_version: null
   linux_distibution: null
   os_name: null
   os_type: null
   os_versionName: null
   os_versionNumber: null

我错过了什么?

ServiceStack.Text and DeserializeFromString

我相信您希望使用以下内容来反序列化JSON字符串:

ServiceStack.Text.JsonSerializer.DeserializeFromString<UserAgentInfo>(json)

TypeSerializer用于JSV格式(jsoncsv-JSV序列化程序)。

相关文章:
  • 没有找到相关文章