从json内容写一个c#类 [英] Write a c# class from a json content

查看:80
本文介绍了从json内容写一个c#类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





如何为此内容编写C#:

< a href =https://data.flightradar24.com/zones/fcgi/feed.js?bounds=-10.756607312117689,-13.287152086983411,-78.19395795214143,-75.78369140625&faa=1&mlat=1&flarm=1&adsb = 1&安培; GND = 1&安培;空气= 1&安培;车辆= 1&安培;估计= 1&安培;最大生存周期= 900安培;滑翔= 1&安培;统计= 1&安培;> https://data.flightradar24.com/zones/fcgi/feed.js ?界限= -10.756607312117689,-13.287152086983411,-78.19395795214143,-75.78369140625&安培; FAA = 1&安培; MLAT = 1&安培; flarm防碰撞系统= 1&安培; ADSB = 1&安培; GND = 1&安培;空气= 1&安培;车辆= 1&安培;估计= 1&安培;最大生存周期= 900& gliders = 1& stats = 1& [ ^ ]

谢谢



我尝试了什么:



我试图使用http://json2csharp.com/但是第一个值被认为是一个Id并且没有很好用。

解决方案

源数据的结构很复杂 - 根对象似乎将静态属性与数据行的键组合在一起。



处理这些数据的最简单方法是使用 JSON.NET [ ^ ]。您可以将其添加为NuGet包 - 搜索JSON.NET或Newtonsoft.Json,它们都指向同一个包: JSON.NET | NuGet Gallery [ ^ ]



(不要被Newtonsoft网站标题中的定价链接推迟;这只适用于网站上的其他产品.JSON.NET是完全免费,即使您在商业应用中使用它。)



以下类将允许您从您提供的链接反序列化数据:

 使用系统; 
使用 System.Collections.Generic;
使用 Newtonsoft.Json;
使用 Newtonsoft.Json.Linq;

public sealed class 统计
{
public IDictionary< string,int>总计{获取; set ; }
public IDictionary< string,int>可见{获取; set ; }
}

public sealed class RootObject
{
private IDictionary< string,JToken> _原始数据;
private IDictionary< string, object [] > _data;

[JsonProperty( full_count)]
public int FullCount { get ; set ; }
public int 版本{ get ; set ; }
public 统计信息统计信息{ get ; set ; }

[JsonExtensionData]
private IDictionary< string,JToken> RawData
{
get
{
return _rawData ;
}
set
{
_rawData = value ;
_data = null ;
}
}

[JsonIgnore]
public IDictionary< string, object [] > 数据
{
get
{
if (_data == null
{
if (_rawData == null || _rawData.Count == 0
{
_data = new Dictionary< string, object [] > ();
}
else
{
_data = _rawData.ToDictionary(pair = > pair.Key,pair = > pair.Value.ToObject< object [] > ());
}
}

return _data;
}
}
}



正如我所说,这比普通的JSON类更复杂。要将动态属性反序列化为数组,我们必须有一个字典来包含任何未知属性,使用 [JsonExtensionData] 属性进行修饰。



该字典中的值将是 JToken objects - 一个用于表示动态JSON的类。由于我们知道值将是对象数组,我们将原始字典转换为值为对象数组的字典。



有了这些类,你应该能够解析JSON数据:

 使用 var  client =  new  System.Net.WebClient())
{
string json = client.DownloadString(url);
RootObject result = JsonConvert.DeserializeObject< RootObject>(json);
// 在此处执行结果...
}


请参阅我过去的答案,其中引用的答案:我需要帮助进行json转换 [ ^ ]。



- SA

Hi,

How to write the C# for this content :
https://data.flightradar24.com/zones/fcgi/feed.js?bounds=-10.756607312117689,-13.287152086983411,-78.19395795214143,-75.78369140625&faa=1&mlat=1&flarm=1&adsb=1&gnd=1&air=1&vehicles=1&estimated=1&maxage=900&gliders=1&stats=1&[^]
Thanks

What I have tried:

I tried to use http://json2csharp.com/ but the first value is suppsed to be an Id and it's not well treadted.

解决方案

This is complicated by the structure of the source data - the root object seems to combine static properties with keys for data rows.

The easiest way to work with this data will be to use JSON.NET[^]. You can add it as a NuGet package - search for either "JSON.NET" or "Newtonsoft.Json", which will both point to the same package: JSON.NET | NuGet Gallery[^]

(Don't be put off by the "Pricing" link in the Newtonsoft site header; that only applies to the other products on the site. JSON.NET is totally free, even if you're using it in a commercial application.)

The following classes will let you deserialize the data from the link you provided:

using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public sealed class Stats
{
    public IDictionary<string, int> Total { get; set; }
    public IDictionary<string, int> Visible { get; set; }
}

public sealed class RootObject
{
    private IDictionary<string, JToken> _rawData;
    private IDictionary<string, object[]> _data;
    
    [JsonProperty("full_count")]
    public int FullCount { get; set; }
    public int Version { get; set; }
    public Stats Stats { get; set; }
    
    [JsonExtensionData]
    private IDictionary<string, JToken> RawData 
    { 
        get
        {
            return _rawData;
        }
        set
        {
            _rawData = value;
            _data = null;
        }
    }
    
    [JsonIgnore]
    public IDictionary<string, object[]> Data
    {
        get
        {
            if (_data == null)
            {
                if (_rawData == null || _rawData.Count == 0)
                {
                    _data = new Dictionary<string, object[]>();
                }
                else
                {
                    _data = _rawData.ToDictionary(pair => pair.Key, pair => pair.Value.ToObject<object[]>());
                }
            }
            
            return _data;
        }
    }
}


As I said, this is more complicated than a normal JSON class. To get the dynamic properties deserialized into an array, we have to have a dictionary to contain any unknown properties, decorated with the [JsonExtensionData] attribute.

The values in that dictionary will be JToken objects - a class designed to represent dynamic JSON. Since we know the values will be arrays of objects, we convert the raw dictionary to one where the values are object arrays.

With these classes in place, you should then be able to parse the JSON data:

using (var client = new System.Net.WebClient())
{
    string json = client.DownloadString(url);
    RootObject result = JsonConvert.DeserializeObject<RootObject>(json);
    // Do something with the result here...
}


Please see my past answer, the answers referenced in it: I need help with a json conversion[^].

—SA


这篇关于从json内容写一个c#类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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