2012-01-17 141 views
0

我试图解析一个JSON响应字符串到我的类对象..我无法弄清楚这一点,我需要一些帮助。反序列化与列表对象

我使用json.net参考,但我cant't找到我要找:(

的json:

{ 

"@companyName": "Company Name", 
"@version": "1.0", 
"@generatedDate": "3/1/10 2:10 PM", 
"application": [ 
    { 
     "@name": "Application #1 name", 
     "@apiKey": "1234", 
     "@createdDate": "2010-03-01", 
     "@platform": "Platform name" 
    }, 
    { 
     "@name": "Application #1 name", 
     "@apiKey": "1234", 
     "@createdDate": "2010-03-01", 
     "@platform": "Platform name" 
    } 
] 
} 

我对JSON根类是:

public class RootObject 
{ 
    [JsonProperty] 
    public string companyName { get; set; } 

    [JsonProperty] 
    public string version { get; set; } 

    [JsonProperty] 
    public string generatedDate { get; set; } 

    [JsonProperty] 
    public List<Application> application { get; set; } 
} 

我的子类(应用程序列表):

public class Application 
{ 
    [JsonProperty] 
    public string name { get; set; } 

    [JsonProperty] 
    public string apiKey { get; set; } 

    [JsonProperty] 
    public string createdDate { get; set; } 

    [JsonProperty] 
    public string platform { get; set; } 
} 

解析它,我有现在下面的代码:

 JObject obj = JObject.Parse(e.Result); 
     applications = new RootObject 
      { 
       companyName = (string) obj["companyName"], 
       version = (string) obj["version"], 
       generatedDate = (string) obj["generatedDate"], 
       application = ???????? (how to make a list here?) 

      } 

提前感谢!

回答

4

更改您的类定义如下

public class RootObject 
{ 
    [JsonProperty("@companyName")] 
    public string companyName { get; set; } 

    [JsonProperty("@version")] 
    public string version { get; set; } 

    [JsonProperty("@generatedDate")] 
    public string generatedDate { get; set; } 

    public List<Application> application { get; set; } 
} 

public class Application 
{ 
    [JsonProperty("@name")] 
    public string name { get; set; } 

    [JsonProperty("@apiKey")] 
    public string apiKey { get; set; } 

    [JsonProperty("@createdDate")] 
    public string createdDate { get; set; } 

    [JsonProperty("@platform")] 
    public string platform { get; set; } 
} 

和反序列化

var rootObj = JsonConvert.DeserializeObject<RootObject>(myjson); 
+0

“@”字符的好处L.B.当我在回答问题时,我甚至没有注意到他们。 :) – jdonley83

+0

无法将JSON对象反序列化为类型'System.Collections.Generic.List'1 [应用程序] –

+1

它可以在我的机器上正常工作,但您也可以尝试'public Application [] application {get;组; }' –

0

你可以试试下面的代码并报告任何问题:

RootObject applications = JsonConvert.DeserializeObject<RootObject>(e.Result); 
+0

无法将JSON对象反序列化为类型'System.Collections.Generic.List'1 [Flurry_Analytics.classes.Application]'。 –

+0

你的应用程序类是否标记为可序列化? –

+0

不,它没有标记为可序列化 –

0

一个项目,我的工作偶尔会使用Json.Net。这是一个很棒的图书馆。 我会使用JsonConvert.DeserializeObject方法。

在你的情况我会尝试这样的事:

var result = JsonConvert.DeserializeObject<RootObject>(yourJsonString); 

这应该照顾它。

+0

我在应用程序列表中获取错误:( 无法将JSON对象反序列化到类型'System.Collections.Generic.List'1 [应用程序] –

+0

@ user917769我得到它在一个测试应用程序工作。我不得不添加额外的参数给JsonProperty属性,如在LB的答案,虽然为了让这个例子与这篇文章中的JSON一起工作 – jdonley83

+0

或者你可以删除'@ 'json的字符,所以json属性名称与类属性名称匹配 – jdonley83