gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:21:29 25 4
gpt4 key购买 nike

如何使用类修饰在嵌套对象中选择 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://image.bing.com/Mercury.jpg",
"neighbor": [
{
"ring": "Two",
"moons": 2
}
],
"estimatedLocationDate": "2014-10-01T14:00:00.000"
},

最佳答案

您发布的 JSON 无效。您可以在 JsonLint 验证您的 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://image.bing.com/Mercury.jpg",
"neighbor": [
{
"ring": "Two",
"moons": 2
}
],
"estimatedLocationDate": "2014-10-01T14:00:00.000"
}
}
]
}

阅读整个 JSON 的简单方法是将其反序列化 到适当的类层次结构。如果您还没有,您可以按照以下步骤在 Visual Studio 中创建

  • Copy your JSON data
  • Create a new empty class in VS
  • VS > Edit > Paste Special > Paste JSON As Classes

这是生成的类

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<PlanetRoot>(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<DateTime>();

关于c# - 如何在 C# 中使用 Json.NET 从 JSON 访问嵌套对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33886361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com