neneo
neneo

Reputation: 646

Deserialize JSON Got Error Using Newtonsoft.Json Xamarin C#

i got error when i'm trying to deserialize JSON because my JSON is not single data (it's like array), then i try use List<> but it said Cannot convert from System.Collections.Generic.List<string>' to 'string

here is the example of JSON data :

[
    {
        "no":"73",
        "nama":"Nicosia Lady",
        "uk":"37 - 40",
        "fotou":"73-u.jpg",
        "bahan":"Canvas",
        "poin":"120",
        "harga":"119900.00",
        "warna1":"Green",
        "warna2":"Grey",
        "warna3":"Navy",
        "warna4":"Maroon",
        "warna5":null
    },
    {
        "no":"78",
        "nama":"Minsk Man",
        "uk":"38 - 43",
        "fotou":"78-u.jpg",
        "bahan":"Canvas",
        "poin":"140",
        "harga":"141800.00",
        "warna1":"Black White",
        "warna2":"Brown BCoffee",
        "warna3":"Navy Orange",
        "warna4":"Grey Navy",
        "warna5":null
    },
]

this is my class :

class user
    {
        public int no { get; set; }
        public string nama { get; set; }
        public string uk { get; set; }
        public string fotou { get; set; }
        public string bahan { get; set; }
        public int poin { get; set; }
        public int harga { get; set; }
        public string warna1 { get; set; }
        public string warna2 { get; set; }
        public string warna3 { get; set; }
        public string warna4 { get; set; }
        public string warna5 { get; set; }
    }

this is the script :

button.Click += async (sender, e) =>
           {
 string url = "http://myapi.com/url/example/";
 List<user> userList = JsonConvert.DeserializeObject<List<user>>(await FetchUserAsync(url));//got error on await FetchUserAsync(url)

           // txtHasil.Text = userList.nama;
        };

and this :

private async Task<List<string>> FetchUserAsync(string url)
        {
            // Create an HTTP web request using the URL:
             HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
            //HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.ContentType = "application/json";
            request.Method = "GET";

            // Send the request to the server and wait for the response:
            using (WebResponse response = await request.GetResponseAsync())
            {
                // Get a stream representation of the HTTP web response:
                using (var sr = new StreamReader(response.GetResponseStream()))
                {
                    string strContent = sr.ReadToEnd();
                    return strContent;// got error on this line
                }
            }
        }

Upvotes: 0

Views: 159

Answers (1)

Suhas Kulkarni
Suhas Kulkarni

Reputation: 263

Return string instead of List<string>.

Another rectification (but not an error) Change the int to string, since you are getting the string value for below properties:

public string no { get; set; }
public string poin { get; set; }
public string harga { get; set; }

Upvotes: 4

Related Questions