【问题标题】:Deserialize to Object with a List使用列表反序列化为对象
【发布时间】:2012-01-17 15:06:30
【问题描述】:

我正在尝试将 JSON 响应字符串解析为我的类对象。我无法弄清楚这一点,我需要一些帮助。

我使用 json.net 参考,但找不到我要找的东西:(

我的 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?)

            }

提前致谢!

【问题讨论】:

    标签: c# json list windows-phone-7 json.net


    【解决方案1】:

    如下更改你的类定义

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

    【讨论】:

    • 很好地抓住了“@”字符 L.B.当我在回答时,我什至没有注意到它们。 :)
    • 无法将 JSON 对象反序列化为类型 'System.Collections.Generic.List`1 [Application]
    • 它在我的机器上运行良好,但你也可以试试public Application[] application { get; set; }
    【解决方案2】:

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

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

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

    这应该会解决它。

    【讨论】:

    • 我在应用程序列表中遇到错误 :( 无法将 JSON 对象反序列化为类型 'System.Collections.Generic.List`1 [Application]
    • @user917769 我让它在测试应用程序中工作。我不得不将额外的参数添加到 JsonProperty 属性中,就像 L.B 的答案一样,但要让这个示例与这篇文章中的 json 一起使用。
    • 或者您可以从 json 中删除“@”字符,以便 json 属性名称与类属性名称匹配
    • 我会在接下来的一个小时内尝试
    • 对我来说它目前不工作......也许它必须对序列化做些什么?您可以发布您的代码示例吗?
    【解决方案3】:

    您能否尝试以下代码并报告任何问题:

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

    【讨论】:

    • 无法将 JSON 对象反序列化为类型“System.Collections.Generic.List`1[Flurry_Analytics.classes.Application]”。
    • 我无法将其标记为可序列化,因为它在 Windows Phone 7 Silverlight 中是不可能的
    猜你喜欢
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 2016-10-12
    相关资源
    最近更新 更多