简体   繁体   中英

c# Parsing json to object

I need some help to parse the json rest from IsThereASale API to a c# object but I got stuck because of the json layout:

https://del.dog/orfelefane.json

What I need here is the info from the data array.

Here is how i get the json response:

 var client = new RestClient("https://api.isthereanydeal.com/");
 client.UseNewtonsoftJson();

 var request = new RestRequest("https://api.isthereanydeal.com/v01/deals/list/?key=" + Config.apiKey + "&sort=time");
 var json = JsonConvert.DeserializeObject<Dictionary<string, string>>(response.Content);

As you can notice above I've tried to convert it to a dictionary, but with no success.

You can use online converter for this

public partial class Orfelefane
{
    public Meta Meta { get; set; }
    public Data Data { get; set; }
}

public partial class Data
{
    public long Count { get; set; }
    public List<List> List { get; set; }
    public DataUrls Urls { get; set; }
}

public partial class List
{
    public string Plain { get; set; }
    public string Title { get; set; }
    public double PriceNew { get; set; }
    public double PriceOld { get; set; }
    public long PriceCut { get; set; }
    public long Added { get; set; }
    public long? Expiry { get; set; }
    public Shop Shop { get; set; }
    public List<Drm> Drm { get; set; }
    public ListUrls Urls { get; set; }
}

public partial class Shop
{
    public Id Id { get; set; }
    public Name Name { get; set; }
}

public partial class ListUrls
{
    public Uri Buy { get; set; }
    public Uri Game { get; set; }
}

public partial class DataUrls
{
    public Uri Deals { get; set; }
}

public partial class Meta
{
    public string Currency { get; set; }
}

public enum Drm { DrmFree, Steam };

public enum Id { Bundlestars, Gog, Itchio, Steam };

public enum Name { Fanatical, Gog, ItchIo, Steam };

lastly

var json = JsonConvert.DeserializeObject<Orfelefane>(response.Content);

EDIT:

If your json is not stronglytyped, I suggest you to use

var json = JsonConvert.DeserializeObject<dynamic>(response.Content);

If only the specific part of your json is not static then you can replace that part with dynamic

let say that the content of the List is dynamic inside data, then change

public partial class Data
{
    public long Count { get; set; }
    public List<List> List { get; set; }
    public DataUrls Urls { get; set; }
}

to

public partial class Data
{
    public long Count { get; set; }
    //OR public dynamic List {get; set;}
    public List<dynamic> List { get; set; } 
    public DataUrls Urls { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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