Gea*_*olt 2 .net c# json json.net
如何使用类装饰在嵌套对象中选择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; }
Run Code Online (Sandbox Code Playgroud)
而这就是 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"
},
Run Code Online (Sandbox Code Playgroud)
在JSON您发布无效.您可以JSON在JsonLint验证您的 身份.
我们假设下面是您的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"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
阅读整体的简单方法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; }
}
Run Code Online (Sandbox Code Playgroud)
现在,您可以轻松阅读整个对象并访问您的estimatedLocationDate类似内容
var jsonString = File.ReadAllText(@"C:\YourDirectory\YourFile.json");
PlanetRoot planet = JsonConvert.DeserializeObject<PlanetRoot>(jsonString);
DateTime estimatedLocationDate = planet.planet.First().product.estimatedLocationDate;
Run Code Online (Sandbox Code Playgroud)
或者,如果你不想读取整个对象,可以直接读取使用该属性Json.NET LINQ到JSON这样
var jObject = JObject.Parse(jsonString);
var estLocDate = jObject["planet"][0]["product"]["estimatedLocationDate"].ToObject<DateTime>();
Run Code Online (Sandbox Code Playgroud)