The JSON value could not be converted to System.Collections.Generic.List`

Jassim Al Rahma 1,086 Reputation points
2021-11-26T19:23:17.007+00:00

Hi,

I am trying to read OneSignal REST's Json but getting below error:

The JSON value could not be converted to System.Collections.Generic.List`1[MyOneSignal.HomePage+Root]. Path: $ | LineNumber: 0 | BytePositionInLine: 1.

Here is my code:

var client = new HttpClient();

UriBuilder builder = new UriBuilder("https://onesignal.com/api/v1/notifications");
builder.Query = "app_id=" + SecureStorage.GetAsync("AppID").Result;

client.DefaultRequestHeaders.Add("authorization", "Basic " + SecureStorage.GetAsync("AppAuthID").Result);

var response = await client.GetAsync(builder.Uri);

var result = await response.Content.ReadAsStringAsync();

List<Root> data = JsonSerializer.Deserialize<List<Root>>(result);

ListViewBrowseNotifications.ItemsSource = data;

and the Class is here:

public class Contents
{
    public string en { get; set; }
}

public class Data
{
    public string business { get; set; }
    public string ad { get; set; }
}

public class Headings
{
    public string en { get; set; }
}

public class ApnsAlert
{
}

public class Ios
{
    public int successful { get; set; }
    public int failed { get; set; }
    public int errored { get; set; }
    public int converted { get; set; }
    public int received { get; set; }
}

public class Android
{
    public int successful { get; set; }
    public int failed { get; set; }
    public int errored { get; set; }
    public int converted { get; set; }
    public int received { get; set; }
}

public class PlatformDeliveryStats
{
    public Ios ios { get; set; }
    public Android android { get; set; }
}

public class Notification
{
    public object adm_big_picture { get; set; }
    public object adm_group { get; set; }
    public object adm_group_message { get; set; }
    public object adm_large_icon { get; set; }
    public object adm_small_icon { get; set; }
    public object adm_sound { get; set; }
    public object spoken_text { get; set; }
    public object alexa_ssml { get; set; }
    public object alexa_display_title { get; set; }
    public object amazon_background_data { get; set; }
    public object android_accent_color { get; set; }
    public object android_group { get; set; }
    public object android_group_message { get; set; }
    public object android_led_color { get; set; }
    public string android_sound { get; set; }
    public int? android_visibility { get; set; }
    public string app_id { get; set; }
    public object big_picture { get; set; }
    public string buttons { get; set; }
    public bool? canceled { get; set; }
    public object chrome_big_picture { get; set; }
    public object chrome_icon { get; set; }
    public object chrome_web_icon { get; set; }
    public object chrome_web_image { get; set; }
    public object chrome_web_badge { get; set; }
    public object content_available { get; set; }
    public Contents contents { get; set; }
    public int converted { get; set; }
    public Data data { get; set; }
    public object delayed_option { get; set; }
    public object delivery_time_of_day { get; set; }
    public int errored { get; set; }
    public List<object> excluded_segments { get; set; }
    public int failed { get; set; }
    public object firefox_icon { get; set; }
    public string global_image { get; set; }
    public Headings headings { get; set; }
    public string id { get; set; }
    public object include_player_ids { get; set; }
    public List<string> include_external_user_ids { get; set; }
    public List<string> included_segments { get; set; }
    public object thread_id { get; set; }
    public int? ios_badgeCount { get; set; }
    public object ios_badgeType { get; set; }
    public object ios_category { get; set; }
    public string ios_interruption_level { get; set; }
    public double? ios_relevance_score { get; set; }
    public string ios_sound { get; set; }
    public ApnsAlert apns_alert { get; set; }
    public object target_content_identifier { get; set; }
    public bool? isAdm { get; set; }
    public bool? isAndroid { get; set; }
    public bool? isChrome { get; set; }
    public bool? isChromeWeb { get; set; }
    public object isAlexa { get; set; }
    public bool? isFirefox { get; set; }
    public bool? isIos { get; set; }
    public bool? isSafari { get; set; }
    public bool? isWP { get; set; }
    public bool? isWP_WNS { get; set; }
    public bool? isEdge { get; set; }
    public bool? isHuawei { get; set; }
    public bool? isSMS { get; set; }
    public object large_icon { get; set; }
    public int? priority { get; set; }
    public int queued_at { get; set; }
    public int remaining { get; set; }
    public int send_after { get; set; }
    public int? completed_at { get; set; }
    public object small_icon { get; set; }
    public int successful { get; set; }
    public object received { get; set; }
    public object tags { get; set; }
    public object filters { get; set; }
    public object template_id { get; set; }
    public int? ttl { get; set; }
    public string url { get; set; }
    public object web_url { get; set; }
    public object app_url { get; set; }
    public object web_buttons { get; set; }
    public object web_push_topic { get; set; }
    public object wp_sound { get; set; }
    public object wp_wns_sound { get; set; }
    public PlatformDeliveryStats platform_delivery_stats { get; set; }
    public object ios_attachments { get; set; }
    public object huawei_sound { get; set; }
    public object huawei_led_color { get; set; }
    public object huawei_accent_color { get; set; }
    public string huawei_visibility { get; set; }
    public object huawei_group { get; set; }
    public object huawei_group_message { get; set; }
    public object huawei_channel_id { get; set; }
    public object huawei_existing_channel_id { get; set; }
    public object huawei_small_icon { get; set; }
    public object huawei_large_icon { get; set; }
    public object huawei_big_picture { get; set; }
    public string huawei_msg_type { get; set; }
    public object throttle_rate_per_minute { get; set; }
    public object sms_from { get; set; }
    public object sms_media_urls { get; set; }
    public object name { get; set; }
    public object email_click_tracking_disabled { get; set; }
    public object fcap_group_ids { get; set; }
    public string fcap_status { get; set; }
}

public class Root
{
    public int total_count { get; set; }
    public int offset { get; set; }
    public int limit { get; set; }
    public List<Notification> notifications { get; set; }
}

Kindly help..

Thanks,
Jassim

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
4,813 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
6,984 questions
No comments
{count} votes

Accepted answer
  1. P a u l 7,566 Reputation points
    2021-11-26T19:28:41.25+00:00

    I believe there's only one root in the response so if you change this:

    List<Root> data = JsonSerializer.Deserialize<List<Root>>(result);
    

    To:

    Root data = JsonSerializer.Deserialize<Root>(result);
    

    Based on the docs: https://documentation.onesignal.com/reference/view-notifications


0 additional answers

Sort by: Most helpful