如何在xamarin android上使用c#解析和显示Json数据?

时间:2017-04-23 13:29:11

标签: c# android json xamarin

我正在尝试使用c#提取Json数据并使用xamarin android显示它。我尝试过这样的代码来发送请求

private async Task<JsonValue> GetTeams(string url)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
            request.ContentType = "application/json";
            request.Method = "GET";
            request.Headers["X-Auth-Token"] = key;

            using (WebResponse response = await request.GetResponseAsync())
            {
                // Get a stream representation of the HTTP web response:
                using (Stream stream = response.GetResponseStream())
                {
                    // Use this stream to build a JSON document object:
                    JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream));
                    Console.Out.WriteLine("Response: {0}", jsonDoc.ToString());

                    // Return the JSON document:
                    return jsonDoc;
                }
            }
        }

以及以下解析和显示

private void ParseAndDisplay(JsonValue json)
        {
            dynamic teamData = json["teams"];

            TextView name = FindViewById<TextView>(Resource.Id.txtName);

            foreach (var team in teamData)
            {
                //name.Text = team["name"];
                Console.WriteLine("\r\n {0}", team["name"]);
            }
        }

运行时手机上没有显示任何内容,但我可以看到输出中的所有数据。 输出的链接是http://api.football-data.org/v1/competitions/426/teams

0 个答案:

没有答案