如何使用C#中的Json.NET从JSON访问嵌套对象

如何使用类装饰在嵌套对象中选择json值“estimatedLocationDate” ? 属性“estimatedLocationDate”始终返回null而不是值2015-10-01T14:00:00.000 。 其他修饰值返回正确的值。

这是我的C#

 public string id { get; set; } public string name { get; set; } [JsonProperty("publishedDate")] public string publishdate { get; set; } [JsonProperty("estimatedLocationDate")] public string estimatedLocationDate{ get; set; } [JsonProperty("createdTime")] public string createtime { get; set; } [JsonProperty("lastUpdatedTime")] public string lastupdate { get; set; } 

这是JSON

 "planet": [ { "id": "123456", "planetid": "en-us/Jupiter-mars/---main", "name": "The planet Mercury", "description": "This is placeholder for the description", "publishedDate": "2013-10-14T23:30:00.000", "createtime": "2012-03-01T14:00:00.000", "product": { "moreid": "1427-48-bd-9-113", "color": "200", "imageUrl": "http://sofzh.miximages.com/c%23/images", "neighbor": [ { "ring": "Two", "moons": 2 } ], "estimatedLocationDate": "2014-10-01T14:00:00.000" }, 

你的类应该看起来像这样(虽然这是不完整的):

 class Planet { [JsonProperty("planet")] PlanetInfo[] planet { get; set; } } class Product { [JsonProperty("estimatedLocationDate")] string estimatedLocationDate {get;set;} } class PlanetInfo { public string id { get; set; } public string name { get; set; } [JsonProperty("publishedDate")] public string publishdate { get; set; } [JsonProperty("estimatedLaunchDate")] public string estimatedLaunchDate { get; set; } [JsonProperty("createdTime")] public string createtime { get; set; } [JsonProperty("lastUpdatedTime")] public string lastupdate { get; set; } [JsonProperty("product")] public Product product { get; set; } } 

您发布的JSON无效。 您可以在JsonLintvalidation您的JSON

我们假设下面是您的JSON数据。

 { "planet": [ { "id": "123456", "planetid": "en-us/Jupiter-mars/---main", "name": "The planet Mercury", "description": "This is placeholder for the description", "publishedDate": "2013-10-14T23:30:00.000", "createtime": "2012-03-01T14:00:00.000", "product": { "moreid": "1427-48-bd-9-113", "color": "200", "imageUrl": "http://sofzh.miximages.com/c%23/images", "neighbor": [ { "ring": "Two", "moons": 2 } ], "estimatedLocationDate": "2014-10-01T14:00:00.000" } } ] } 

读取整个JSON的简单方法是将其deserialize适当的类层次结构。 如果您还没有,可以在Visual Studio中创建以下步骤

  • 复制您的JSON数据
  • 在VS中创建一个新的空类
  • VS>编辑>选择性粘贴>将JSON粘贴为类

这是生成的类

 public class PlanetRoot { public Planet[] planet { get; set; } } public class Planet { public string id { get; set; } public string planetid { get; set; } public string name { get; set; } public string description { get; set; } public Product product { get; set; } [JsonProperty("publishedDate")] public string publishdate { get; set; } [JsonProperty("createdTime")] public string createtime { get; set; } } public class Product { public string moreid { get; set; } public string color { get; set; } public string imageUrl { get; set; } public Neighbor[] neighbor { get; set; } public DateTime estimatedLocationDate { get; set; } } public class Neighbor { public string ring { get; set; } public int moons { get; set; } } 

现在,很容易读取整个对象并像这样访问您的estimatedLocationDate

 var jsonString = File.ReadAllText(@"C:\YourDirectory\YourFile.json"); PlanetRoot planet = JsonConvert.DeserializeObject(jsonString); DateTime estimatedLocationDate = planet.planet.First().product.estimatedLocationDate; 

或者,如果您不想读取整个对象,可以使用Json.NET Linq-to-JSON直接读取该属性

 var jObject = JObject.Parse(jsonString); var estLocDate = jObject["planet"][0]["product"]["estimatedLocationDate"].ToObject(); 

由于product代表另一个对象,因此您需要为它创建一个类:

 public class Planet { public string id { get; set; } public string name { get; set; } [JsonProperty("publishedDate")] public string publishdate { get; set; } [JsonProperty("estimatedLaunchDate")] public string estimatedLaunchDate { get; set; } [JsonProperty("createdTime")] public string createtime { get; set; } [JsonProperty("lastUpdatedTime")] public string lastupdate { get; set; } public Product product { get; set; } } public class Product { public DateTime estimatedLocationDate { get; set; } /* Other members, if necessary */ } string result = JsonConvert.DeserializeObject(jsonString).product.estimatedLocationDate; 

注意:

  • 您发布的JSON无效。 假设您没有复制整个JSON,并忽略了这个,因为“其他修饰的值会返回正确的值”。
  • 最好将UpperCamelCase名称赋予C#属性

您可以为Product使用内部类,并使用类似的属性公开它。

 public class JsonNet { public static string jsonString = @"{ ""inner"" : { ""name"" : ""myname""}}"; public static Test GetTest() { return JsonConvert.DeserializeObject(jsonString); } } public class Inner { public string Name { get; set; } } public class Test { public Inner Inner { get; set; } public string Name { get { return Inner.Name; } set { Inner.Name = value; } } }