문제

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:

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:

"{\"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#

"{"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:

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

The TypeSerializer is for the JSV format (json-csv-jsv-serializers).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top