0

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
                }
            }
        }
2
  • This is not related to json serialization. Change return type of FetchUserAsync to Task<string> Commented Jul 13, 2016 at 11:41
  • sorry about that and thanks it works Commented Jul 14, 2016 at 4:32

1 Answer 1

4

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; }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.