如何在C#中使用Json.NET从JSON访问嵌套对象 [英] How to access nested object from JSON with Json.NET in C#

查看:477
本文介绍了如何在C#中使用Json.NET从JSON访问嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I select the json value "estimatedLocationDate" within a nested object using class decoration? The property "estimatedLocationDate" always returns null instead of the value 2015-10-01T14:00:00.000. The other decorated values return the proper values.

这是我的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"
        },

推荐答案

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

Your classes should look something like this (this is incomplete though):

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

这篇关于如何在C#中使用Json.NET从JSON访问嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆