Forum Cross Platform with Xamarin

Unable to parse JSON array from API in Xamarin.Forms to ListView

Hey folks, I´ve been trying this for a while, but I´m quite new to Xamarin/json.NET, I already have the models defined but I´m failing on how to get the data from the API:

My Model:

using System.Collections.ObjectModel;
using Newtonsoft.Json;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using System.Net.Http;


public class Prts
        {
            [JsonProperty("CategoryName")]
            public string CategoryName { get; set; }
            [JsonProperty("PartnerName")]
            public string PartnerName { get; set; }
            [JsonProperty("Zip")]
            public string Zip { get; set; }
            [JsonProperty("City")]
            public string City { get; set; }
        }

My attempt to get the json from the API:

public class websrvc
        {
            private const string Url = "192.168.1.103:8088/api/partners/2"; //hosted locally, IIS, I can open and view it.
            private HttpClient httpClient1 = new HttpClient();

        //how to request the array that´s being displayed by the Url.

            ObservableCollection<Prts> parteners = new ObservableCollection<Prts>();
            MyListView2.ItemsSource = parteners;

        }

Nevertheless, on the websrvc class, I don´t know how whether I have to use the JsonTextReader from Newtonsoft.Json or if I can read it in another way, what I need is something that works similar to JsonArrayRequest.

Best Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    Accepted Answer

    @tkruise said:

    @ColeX said:
    First , ensure that the string returned web service should be valid json formatting , just like

    [
    {
    "CategoryName ": 1,
    "PartnerName ": "cole1",
    "Zip ": "123",
    "City": "NewYork",
    },
    {
    "CategoryName ": 2,
    "PartnerName ": "cole2",
    "Zip ": "test2",
    "City": "San Francisco",
    },
    //xxx
    ]
    

    Is it ok if my Json isn´t adding the breaklines?
    It´s returning it like:
    [{"Id":1,"FirstName":"Emil","LastName":"Christensen","Zip":"129391 ","City":"Oslo"},{"Id":2,"FirstName":"Jarno","LastName":"Christensen","Zip":"129391 ","City":"Oslo"},{"Id":3,"FirstName":"Anna","LastName":"Jorg","Zip":"129231 ","City":"Trondheim"},{"Id":4,"FirstName":"Ema","LastName":"Sippa","Zip":"12123 ","City":"Aver"}]

    Of course , use http://json2csharp.com/ to check if your json string valid or not .

Answers

  • tkruisetkruise Member ✭✭

    @ColeX said:
    First , ensure that the string returned web service should be valid json formatting , just like

    [
    {
    "CategoryName ": 1,
    "PartnerName ": "cole1",
    "Zip ": "123",
    "City": "NewYork",
    },
    {
    "CategoryName ": 2,
    "PartnerName ": "cole2",
    "Zip ": "test2",
    "City": "San Francisco",
    },
    //xxx
    ]
    

    Is it ok if my Json isn´t adding the breaklines?
    It´s returning it like:
    [{"Id":1,"FirstName":"Emil","LastName":"Christensen","Zip":"129391 ","City":"Oslo"},{"Id":2,"FirstName":"Jarno","LastName":"Christensen","Zip":"129391 ","City":"Oslo"},{"Id":3,"FirstName":"Anna","LastName":"Jorg","Zip":"129231 ","City":"Trondheim"},{"Id":4,"FirstName":"Ema","LastName":"Sippa","Zip":"12123 ","City":"Aver"}]

  • ColeXColeX Member, Xamarin Team Xamurai
    Accepted Answer

    @tkruise said:

    @ColeX said:
    First , ensure that the string returned web service should be valid json formatting , just like

    [
    {
    "CategoryName ": 1,
    "PartnerName ": "cole1",
    "Zip ": "123",
    "City": "NewYork",
    },
    {
    "CategoryName ": 2,
    "PartnerName ": "cole2",
    "Zip ": "test2",
    "City": "San Francisco",
    },
    //xxx
    ]
    

    Is it ok if my Json isn´t adding the breaklines?
    It´s returning it like:
    [{"Id":1,"FirstName":"Emil","LastName":"Christensen","Zip":"129391 ","City":"Oslo"},{"Id":2,"FirstName":"Jarno","LastName":"Christensen","Zip":"129391 ","City":"Oslo"},{"Id":3,"FirstName":"Anna","LastName":"Jorg","Zip":"129231 ","City":"Trondheim"},{"Id":4,"FirstName":"Ema","LastName":"Sippa","Zip":"12123 ","City":"Aver"}]

    Of course , use http://json2csharp.com/ to check if your json string valid or not .

  • tkruisetkruise Member ✭✭

    OK ColeX, thanks for the fast response, helped me a lot, nonetheless, I got one more doubt, this is the Request, inside the WebInterface class:

    public class WebInterface
            {
                public HttpClient _client;
    
                public WebInterface(HttpClient httpClient)
                {
                    _client = httpClient;
                }
    
                private const string url1 = "http://192.168.1.103:8088/api/partners/2";
    
                public async Task<List<Prts>> RefreshDataAsync()
                {
                    try
                    {
                        var uri = new Uri(string.Format(url1, string.Empty));
                        var response = await _client.GetAsync(uri);
    
                        if (response.IsSuccessStatusCode)
                        {
                            var content = await response.Content.ReadAsStringAsync();
                            List<Prts> MyListView2 = JsonConvert.DeserializeObject<List<Prts>>(content);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error: ", ex);
                    }
                }
    

    I´m getting an strange error for the RefreshDataAsync() method, can´t I put a GetAsync method right there? (I won´t need to check the data, this json will never change)
    ** Severity Code Description Project File Line Suppression State
    Error CS0161 'Partners.WebInterface.RefreshDataAsync()': not all code paths return a value**

Sign In or Register to comment.