ServiceStack.Text和DeserializeFromString

时间:2013-07-17 10:51:20

标签: c# json servicestack servicestack-text

我最承认我可能忘记了一些非常简单的事情,但我无法弄清楚是什么

我有一个班级:

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

我错过了什么?

1 个答案:

答案 0 :(得分:4)

我相信你想使用以下命令来反序列化JSON字符串:

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

TypeSerializer适用于JSV格式(json-csv-jsv-serializers)。