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 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 .

Sign In or Register to comment.