如何解析Windows Store / Windows Phone App中的JSON

时间:2014-05-05 06:47:57

标签: c# .net windows windows-phone-8 windows-store-apps

我的网址返回以下JSON

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

我希望填充List<Genre> genreList; 流派看起来像

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

我写了以下代码,但它根本没有工作

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;
        }

请建议..

这是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);
        }

4 个答案:

答案 0 :(得分:2)

您可以使用Newtonsoft json nuget包,您可以使用一行代码进行解析。

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

其中e.result包含你的回复json。关注here

答案 1 :(得分:1)

HttpClient client = new HttpClient(); string jsonstring = await client.GetStringAsync(webaddress); var obj = JsonConvert.DeserializeObject<RootObject>(jsonString);
获取解析后的字符串后,您可以根据需要存储或使用它。

答案 2 :(得分:0)

尝试

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

获取Http响应

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
}

答案 3 :(得分:0)

这将获得流派类对象列表

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

PS :当您将 jsonString 而不是 jsonString.Result.ToString()作为反序列化的数据时,您将获得Null引用异常