简体   繁体   English

ServiceStack.Text和DeserializeFromString

[英]ServiceStack.Text and DeserializeFromString

I most admit that I'm probably forgetting something really simple, but I can't figure it out what 我最承认我可能忘记了一些非常简单的事情,但我无法弄清楚是什么

I have a class: 我有一节课:

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; }
}

then I get the Json string from a Url and try to deserialize it as: 然后我从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);
}

the json string is: 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\"}"

or for the ones that are not used to C# 或者那些不习惯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"}"

and the returned object is: 返回的对象是:

{...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

What am I missing? 我错过了什么?

I believe you want to use the following to deserialize JSON string: 我相信你想使用以下来反序列化JSON字符串:

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

The TypeSerializer is for the JSV format ( json-csv-jsv-serializers ). TypeSerializer用于JSV格式( json-csv-jsv-serializers )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM