如何将此JSON反序列化为对象?

问题描述:

我正在尝试使用JSON.Net将JSON对象反序列化为一个C#对象。如何将此JSON反序列化为对象?

我想创建目的是MonthlyPerformance其中包含的Type一个列表,其中包含的Categories名单,这反过来又包含Funds列表。他们被定义为:

public class MonthlyPerformance 
{ 
    public List<Type> Types { get; set; } 
} 

public class Type 
{ 
    public int Id { get; set; } 
    public string CountryId { get; set; } 
    public string Name { get; set; } 

    public List<Category> Categories { get; set; } 

    public Type() 
    { 

    } 
} 

public class Category 
{ 
    public int Id { get; set; } 
    public string CountryId { get; set; } 
    public string Name { get; set; } 

    public List<ConfigurationFund> Funds { get; set; } 

    public Category() 
    { 

    } 
} 

public class Fund 
{ 
    public int Id { get; set; } 
    public string CountryId { get; set; } 
    public string Name { get; set; } 

    public Fund() 
    { 

    } 
} 

我认为以下将做到这一点,但事实并非如此。这只是一切空创建Type对象的实例:

var file = File.ReadAllText(filePath); 

var types = JsonConvert.DeserializeObject<Type>(file); 

这是我使用的JSON:

{ 
    "MonthlyPerformance": { 
    "Type": [ 
     { 
     "id": "65", 
     "countryId": "IE", 
     "name": "Irish Domestic Funds (Gross)", 
     "Category": [ 
      { 
      "id": "25003334", 
      "countryId": "IE", 
      "name": "UK Equity", 
      "ConfigurationFund": [ 
       { 
       "id": "25000301", 
       "countryId": "IE", 
       "name": "Aviva Irl UK Equity Fund" 
       }, 
       { 
       "id": "25000349", 
       "countryId": "IE", 
       "name": "New Ireland UK Equity 9" 
       } 
      ] 
      }, 
      { 
      "id": "25003339", 
      "countryId": "IE", 
      "name": "Irish Equity", 
      "Fund": [ 
       { 
       "id": "25000279", 
       "countryId": "IE", 
       "name": "Friends First Irish Equity G" 
       }, 
       { 
       "id": "25000305", 
       "countryId": "IE", 
       "name": "Irish Life Celticscope 2 G" 
       } 
      ] 
      } 
     ] 
     }, 
     { 
     "id": "80", 
     "countryId": "IE", 
     "name": "Irish Individual Pensions", 
     "Category": [ 
      { 
      "id": "25003347", 
      "countryId": "IE", 
      "name": "Asia Pacific Ex-Japan Equity", 
      "Fund": [ 
       { 
       "id": "25001789", 
       "countryId": "IE", 
       "name": "Aviva Irl Pacific Basin Eq" 
       }, 
       { 
       "id": "25002260", 
       "countryId": "IE", 
       "name": "Ir Life Pacific Eq Indexed P" 
       } 
      ] 
      }, 
      { 
      "id": "25003365", 
      "countryId": "IE", 
      "name": "Flexible Equity", 
      "Fund": [ 
       { 
       "id": "25003238", 
       "countryId": "IE", 
       "name": "Friends First Protected Equity Plus Fund S2" 
       }, 
       { 
       "id": "25003267", 
       "countryId": "IE", 
       "name": "Friends First Protected Equity Plus Bond G" 
       } 
      ] 
      } 
     ] 
     } 
    ] 
    } 
} 

什么我需要做的就是这个工作?

编辑成包括MonthlyPerformance

+0

你觉得你的代码是指你所定义的类型或者.net中预定义的“类型”。也看看http://james.newtonking.com/projects/json-net.aspx – DarthVader

+0

嗨@DarthVader,我可以看到'Type'的类型是在我自己的命名空间。即使当我改变它来试图创建一个'MonthlyPerformance'的实例(我已经用这个更新了这个问题),它仍然表现得一样。 – DaveDev

我相信这个问题是在你的JSON字符串的类属性。

"Type": [ ... ] 

应该

"Types": [ ... ] 

Types是应该被反序列化的属性名称,你Type类名来代替。

Type类中的Categories属性也是如此。

我只是从JSON字符串中删除"MonthlyPerformance"根,它的工作就像一个魅力。当然用Json.NET。

下面是修改的JSON与相应的属性名称的片段(注意,类型,类别,资金和缺乏MonthlyPerformance根)

{ 
    "Types": [ 
    { 
    "id": "65", 
    "countryId": "IE", 
    "name": "Irish Domestic Funds (Gross)", 
    "Categories": [ 
     { 
     "id": "25003334", 
     "countryId": "IE", 
     "name": "UK Equity", 
     "Funds": [ 
      { 
      "id": "25000301", 
      "countryId": "IE", 
      "name": "Aviva Irl UK Equity Fund" 
      }, 
      { 
      "id": "25000349", 
      "countryId": "IE", 
      "name": "New Ireland UK Equity 9" 
     } 
    ] 
} 

会想到使用dynamic对象,而不是反序列化到对象? (不宣MonthlyPerformanceTypeCategoryFund

Facebook C# SDK get user language/region

Google Maps v3 geocoding server-side

用法:需要

dynamic jobj = JsonUtils.JsonObject.GetDynamicJsonObject(JsonString); 
foreach (var item in jobj.MonthlyPerformance.Type) 
{ 
    Console.WriteLine(item.name); 
    foreach (var category in item.Category) 
    { 
     Console.WriteLine("\t" + category.name); 
     if (category.ConfigurationFund != null) 
     { 
      foreach (var fund in category.ConfigurationFund) 
      { 
       Console.WriteLine("\t\t" + fund.name); 
      } 
     } 
    } 
} 

Helper类是here

您具有上面的代码你的课堂看起来乍一看是正确的。

我已经使用以下类(JavaScriptSerializer)反序列化JSON获得成功。使用方法如下

using System.Web.Script.Serialization; 

JavaScriptSerializer js = new JavaScriptSerializer(); 
string file = File.ReadAllText(filePath); 
MonthyPerformance json = js.Deserialize<MonthlyPerformance>(file); 

噢,并添加[Serializable接口]为每个类

[Serializable] 
class whatever{} 

你的属性名称不匹配,你的JSON的。我期望序列化失败。

鉴于你发布的JSON,我希望你的c#类看起来像这样。我不熟悉JSON.net,但我会假设他们有一个属性装饰器,它允许您指定C#prop与之匹配的JSON属性的名称。

public class MonthlyPerformance 
{ 
    public List<Type> Type { get; set; } 
} 

public class Type 
{ 
    public int id { get; set; } 
    public string countryId { get; set; } 
    public string Name { get; set; } 

    public List<Category> Category { get; set; } 

    public Type() 
    { 

    } 
} 

public class Category 
{ 
    public int id { get; set; } 
    public string countryId { get; set; } 
    public string name { get; set; } 

    public List<Fund> ConfigurationFund{ get; set; } 

    public Category() 
    { 

    } 
} 

public class Fund 
{ 
    public int id { get; set; } 
    public string countryId { get; set; } 
    public string name { get; set; } 

    public Fund() 
    { 

    } 
} 

这是我使用JSON反序列化...

using System.Web.Script.Serialization; 

namespace blahblah 
{ 
    public partial class AccessTierService : ServiceBase 
    { 
     public static T ia_deserialize_json<T>(string json_string) 
     { 
      try 
      { 
       if ((String.Compare(json_string, null) == 0) || 
        (String.Compare(json_string, "") == 0) || 
        (String.Compare(json_string, String.Empty) == 0)) 
       { 
        return default(T); 
       } 
       JavaScriptSerializer serializer = new JavaScriptSerializer(); 
       return (T)serializer.Deserialize<T>(json_string); 
      } 
      catch (Exception) 
      { 
       return default(T); 
      } 
     } 
    } 
} 

,并把它称为...

login_request = ia_deserialize_json<ol_login_request>(body_string);