简体   繁体   English

如何生成具有动态名称的 JSON 类

[英]How to generate a JSON class with dynamic name

I don't know if there is an existing name for that case, but I'm trying to retrieve data from NASA API ( https://api.nasa.gov/ ) and I have a simple challenge to catch a list of objects near earth.我不知道该案例是否有现有名称,但我正在尝试从 NASA API ( https://api.nasa.gov/ ) 检索数据,并且我有一个简单的挑战来捕获对象列表靠近地球。 Here is the JSON response I have from the GET request I do to "https://api.nasa.gov/neo/rest/v1/feed?...."这是我对“https://api.nasa.gov/neo/rest/v1/feed?..”所做的 GET 请求的 JSON 响应

{
"links": {
    "next": "http://www.neowsapp.com/rest/v1/feed?start_date=2021-07-04&end_date=2021-07-04&detailed=false&api_key=NjgpxgSbYHXyFSBI3HaOhRowtjMZgAKv2t4DMRym",
    "prev": "http://www.neowsapp.com/rest/v1/feed?start_date=2021-07-02&end_date=2021-07-02&detailed=false&api_key=NjgpxgSbYHXyFSBI3HaOhRowtjMZgAKv2t4DMRym",
    "self": "http://www.neowsapp.com/rest/v1/feed?start_date=2021-07-03&end_date=2021-07-03&detailed=false&api_key=NjgpxgSbYHXyFSBI3HaOhRowtjMZgAKv2t4DMRym"
},
"element_count": 6,
"near_earth_objects": {
    "2021-07-03": [
        {
            "links": {
                "self": "http://www.neowsapp.com/rest/v1/neo/3701710?api_key=NjgpxgSbYHXyFSBI3HaOhRowtjMZgAKv2t4DMRym"
            },
            "id": "3701710",
            "neo_reference_id": "3701710",
            "name": "(2014 WF497)",
            "nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=3701710",
            "absolute_magnitude_h": 20.23,
            "estimated_diameter": {
                "kilometers": {

} }

And that's the way it is built in Visual Studio (using the Special Paste option for JSON)这就是它在 Visual Studio 中构建的方式(使用 JSON 的特殊粘贴选项)

 public class NearEarthObject
{
    public Links links { get; set; }
    public int element_count { get; set; }
    public Near_Earth_Objects near_earth_objects { get; set; }
}

public class Links
{
    public string next { get; set; }
    public string prev { get; set; }
    public string self { get; set; }
}

public class Near_Earth_Objects
{
    public _20210703[] _20210703 { get; set; }
}

public class _20210703
{
    public Links1 links { get; set; }
    public string id { get; set; }
    public string neo_reference_id { get; set; }
    public string name { get; set; }
    public string nasa_jpl_url { get; set; }
    public float absolute_magnitude_h { get; set; }
    public Estimated_Diameter estimated_diameter { get; set; }
    public bool is_potentially_hazardous_asteroid { get; set; }
    public Close_Approach_Data[] close_approach_data { get; set; }
    public bool is_sentry_object { get; set; }
}

The question is, inside of the element "near_earth_objects", there is an element called "2021-07-03" (the date of the data I requested), the problem is that I am trying to include it into a DataGridView made in .NET C# (Windows Forms, but that doesn't matters here, I think) and the user wants to get the information by date.问题是,在元素“near_earth_objects”内部,有一个名为“2021-07-03”的元素(我请求的数据的日期),问题是我试图将它包含在. NET C#(Windows 窗体,但这并不重要,我认为)并且用户希望按日期获取信息。 So, "2021-07-03" is a valid member just for one day, and the user should be able to get data from multiple days.所以,“2021-07-03”是一天的有效会员,用户应该可以获取多天的数据。

So, is there a way in C# to get all child objects inside of near_earth_objects without knowing their names since there will be the option to search for asteroids from date X to Y in my application?那么,在 C# 中是否有一种方法可以在不知道名称的情况下获取 Near_earth_objects 中的所有子对象,因为在我的应用程序中可以选择搜索从日期 X 到 Y 的小行星?

use System.Text.Json , JsonNamingPolicy使用System.Text.Json , JsonNamingPolicy

demo code演示代码

    public class DynamicNamePolicy : JsonNamingPolicy
    {
        public override string ConvertName(string name)
        {
            var today = DateTime.Today.ToString("yyyy-MM-dd");
            if (name.Equals("DateData")) //model property name
                return today; //convert to json string property name
            return name;
        }
    }
//data deserialize
string data = ""; //json string
var obj = JsonSerializer.Deserialize<NearEarthObject>(data, new JsonSerializerOptions
{
     PropertyNamingPolicy = new DynamicNamePolicy(),
});

Using System.Text.Json使用System.Text.Json

The API response will map to the following classes API 响应将映射到以下类

public class Neo
{
    public Links Links { get; set; }
    public int ElementCount { get; set; }
    public Dictionary<string, List<NearEarthObject>> NearEarthObjects { get; set; }
}

public class Links
{
    public string Next { get; set; }
    public string Prev { get; set; }
    public string Self { get; set; }
}

public class NearEarthObject
{
    public Links Links { get; set; }
    public string Id { get; set; }
    public string Name { get; set; }
    // Other properties
}

The NearEarthObjects is simply a Dictionary , where the key is the formatted date and value is a List containing NearEarthObject NearEarthObjects只是一个Dictionary ,其中键是格式化的日期,值是一个包含NearEarthObjectList

The PropertyNamingPolicy will allow us to support the API's underscore property naming convention. PropertyNamingPolicy将允许我们支持 API 的下划线属性命名约定。

public class UnderscoreNamingPolicy : JsonNamingPolicy
{
    public override string ConvertName(string name)
    {
        return name.Underscore();
    }
}

Example usage示例用法

// using using System.Text.Json;
var response = await new HttpClient().GetStringAsync(url);
var neo = JsonSerializer.Deserialize<Neo>(response, new JsonSerializerOptions
{
    PropertyNamingPolicy = new UnderscoreNamingPolicy()
});

foreach(var neos in neo.NearEarthObjects)
{
    Console.WriteLine(neos.Key);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM