Click here to Skip to main content
15,889,887 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
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.
Posted
Updated 19-Feb-16 8:39am
v2
Comments
Richard Deeming 19-Feb-16 13:45pm    
If that tool doesn't work, and the built-in "Paste Special -> Paste JSON as Classes" doesn't work, then you'll have to write the classes by hand.

Why not start with the auto-generated classes, and then tidy them up manually? That way, the tool does most of the work for you.
Member 12330910 19-Feb-16 14:07pm    
in fact, I don't know how to write the class for this JSON
yeleswarapu ramakrishna 19-Feb-16 13:49pm    
use newton soft json and parse data using linq
Member 12330910 19-Feb-16 14:08pm    
do you have a sample ? I'm using newtonSoft to serialize / deserialize the JSON string in an object
yeleswarapu ramakrishna 19-Feb-16 16:13pm    
richard has already answered use it some one voted the question one star your question deserves a better rating so i voted five

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:
C#
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:
C#
using (var client = new System.Net.WebClient())
{
    string json = client.DownloadString(url);
    RootObject result = JsonConvert.DeserializeObject<RootObject>(json);
    // Do something with the result here...
}
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 19-Feb-16 14:59pm    
5ed.
—SA
Sascha Lefèvre 19-Feb-16 15:11pm    
+5
Please see my past answer, the answers referenced in it: I need help with a json conversion[^].

—SA
 
Share this answer
 
Comments
Richard Deeming 19-Feb-16 14:42pm    
This one is slightly complicated by the mixture of static and dynamic properties in the root object. I've come up with a solution (see solution 2), but there's probably a simpler way to do it. :)
Sergey Alexandrovich Kryukov 19-Feb-16 14:59pm    
Well, it is exactly what you say, "slightly complicated".
Thank you.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900