Pergunta

The url that I have returns follwing JSON

[{"Id":21,"Name":"Pop","Description":"Pop Music"},{"Id":22,"Name":"Classical","Description":"Classical Music"},{"Id":23,"Name":"Rock","Description":"Rock Music"}]

and I want to populate List<Genre> genreList; where Genre looks like

class Genre
{
  int Id{get;set;}
  string Pop{get;Set;}
  string Description {get;set;}
}

I wrote following code but it is not at all working

public List<Genre> GetAllGenres()
        {
            var client = new HttpClient();
            HttpResponseMessage response =  client.G(new Uri("http://localhost/MusicAPI/api/Genre/GetAllGenres/"));
            var jsonString =  response.Content.ReadAsString();

            List<Genre> list = await Newtonsoft.Json.JsonConvert.DeserializeObject<Genre[]>(jsonString);
            return list;
        }

Please suggest..

Here is the answer given by Jagath

 public GenreItemViewer()
        {
            this.InitializeComponent();
            GetResponse();
        }

        public async void GetResponse()
        {
            var postRequest = (HttpWebRequest)WebRequest.Create("http://localhost/MusicAPI/api/Genre/GetAllGenres/");
            postRequest.Method = "GET";
            postRequest.CookieContainer = new CookieContainer(); ;

            HttpWebResponse postResponse = (HttpWebResponse)await postRequest.GetResponseAsync();
            string response = String.Empty;
            if (postResponse != null)
            {
                var postResponseStream = postResponse.GetResponseStream();
                var postStreamReader = new StreamReader(postResponseStream);

                response = await postStreamReader.ReadToEndAsync();
            }

            List<Genre> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Genre>>(response);
        }
Foi útil?

Solução 3

Try

List<Genre> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Genre>>(jsonString);

For Getting the Http response

var postRequest = (HttpWebRequest)WebRequest.Create(url);               
postRequest.Method = "GET";                
postRequest.CookieContainer = new CookieContainer();;

HttpWebResponse postResponse = (HttpWebResponse)await postRequest.GetResponseAsync();

if (postResponse != null)
{
   var postResponseStream = postResponse.GetResponseStream();
   var postStreamReader = new StreamReader(postResponseStream);

   string response = await postStreamReader.ReadToEndAsync();
    return response;// This is the response
}

Outras dicas

You can use Newtonsoft json nuget package where you can do that parsing using one line of code.

List<Genre> genList = JsonConvert.DeserializeObject<List<Genre>>(e.Result);

in which e.result contains your response json. follow here

HttpClient client = new HttpClient(); string jsonstring = await client.GetStringAsync(webaddress); var obj = JsonConvert.DeserializeObject<RootObject>(jsonString);
After getting the parsed string you can store or use it as you want.

This will get the list of genre class object

List<Genre> genreList= JsonConvert.DeserializeObject<List<Genre>>(jsonString.Result.ToString());

PS : When u give jsonString instead of jsonString.Result.ToString() as data for deserializing, You will get Null reference Exception

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top