API icon indicating copy to clipboard operation
API copied to clipboard

C# try to get information from the endpoints

Open ME9114E opened this issue 6 years ago • 9 comments

Hey,

I am programming for a while, but never tried to make some API's. If Itry to get information by the endpoints I always get fault message. If the endpoint information is only 1 line (1country) I don't get faults but more countries or states I have.

image

thank you in advance

ME9114E avatar Mar 21 '20 12:03 ME9114E

You are getting an array with the objects of countries. You may try deserialize this way List<Idictionary> or else you can create a model class with the properties name matching to properties of each country in the endpoint and put it instead of Idictionary

GowthamF avatar Mar 21 '20 13:03 GowthamF

I'm making a version in C#, too. See my repo and see what i'm doing.

MardSilva avatar Mar 21 '20 13:03 MardSilva

@GowthamF thank you for your respond

also tried this:

class Country and class Country_collection

Country_collection resultCountry = JsonConvert.DeserializeObject<Country_collection>(json);

but same error

ME9114E avatar Mar 21 '20 13:03 ME9114E

Why not create a class, and deserialize in a object?

        Class Country

        [JsonProperty("country")]
        public string Country { get; set; }

        [JsonProperty("cases")]
        public string Cases { get; set; }

        [JsonProperty("todayCases")]
        public string TodayCases { get; set; }

And following line: JsonConvert.DeserializeObject<YourClassesGoesHere>(json)

MardSilva avatar Mar 21 '20 13:03 MardSilva

I really aprreciate your help but still got this error message

public partial class Country_collection { [JsonProperty("country")] public string Country { get; set; }

    [JsonProperty("cases")]
    public long Cases { get; set; }

    [JsonProperty("todayCases")]
    public long TodayCases { get; set; }

    [JsonProperty("deaths")]
    public long Deaths { get; set; }

    [JsonProperty("todayDeaths")]
    public long TodayDeaths { get; set; }

    [JsonProperty("recovered")]
    public long Recovered { get; set; }

    [JsonProperty("active")]
    public long Active { get; set; }

    [JsonProperty("critical")]
    public long Critical { get; set; }

    [JsonProperty("casesPerOneMillion")]
    public long CasesPerOneMillion { get; set; }
}

public partial class Country_collection
{
    public static Country_collection[] FromJson(string json) => JsonConvert.DeserializeObject<Country_collection[]>(json, Corona_stats.Converter.Settings);
}

public static class Serialize
{
    public static string ToJson(this Country_collection[] self) => JsonConvert.SerializeObject(self, Corona_stats.Converter.Settings);
}

internal static class Converter
{
    public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
    {
        MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
        DateParseHandling = DateParseHandling.None,
        Converters =
        {
            new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
        },
    };
}

and

image

ME9114E avatar Mar 21 '20 13:03 ME9114E

Can you put your repo on GitHub?

MardSilva avatar Mar 21 '20 13:03 MardSilva

Corona_stats.Converter.Settings what is this "settings"?

MardSilva avatar Mar 21 '20 13:03 MardSilva

This is not an issue related to the api. Closing. Join our discord server if you really need help :)

ghost avatar Mar 21 '20 13:03 ghost

@ME9114E Let me help you. Drop me a mail [email protected]

GowthamF avatar Mar 21 '20 15:03 GowthamF