从 response.Content.ReadStringASync 中提取数据

Extracting data from response.Content.ReadStringASync

我目前正在尝试从我的 Web 服务中提取数据。我设法使用 HTTPClient 从 Web 服务获得成功响应。但是,我无法提取特定值。例如,我的 JSON 文档读作

{"d":[{"__type":"Info:#website.Model","infoClosingHours":"06:00:00 PM","infoID":1,"infoOpeningDays":"Monday","infoOpeningHours":"09:00:00 AM","infoStatus":"Open"}]}

我想获取 infoOpeningDays,但是我无法获取。

我尝试使用

dynamicObject.GetType().GetProperty("infoOpeningDays").GetValue(dynamicObject, null); 

dynamicObject["infoOpeningDays"]; 

但它一直给我 null。

这是我的代码

private async void GetData(object sender, EventArgs e)
        {
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("ip");

            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));

            try{
                HttpResponseMessage response = client.GetAsync("WebServices/information.svc/GetInformationJSON").Result;
                if (response.IsSuccessStatusCode)
                {
                    string jsonString = await response.Content.ReadAsStringAsync();
                    dynamic dynamicObject = JsonConvert.DeserializeObject(jsonString);
                    //string abc = dynamicObject.IEnumerator.[0].IEnumerator.[0].IEnumerator.[0].IEnumerator.[5].Name;
                    string abc = dynamicObject.GetType().GetProperty("infoOpeningDays").GetValue(dynamicObject, null);
                }
            }
            catch
            {

            }

        }

您应该像这样直接从动态对象访问属性:

dynamic dynamicObject = JsonConvert.DeserializeObject(json);
string infoClosingHours = dynamicObject.d[0].infoClosingHours;

或者这个是一样的

string infoClosingHours = dynamicObject.d[0]["infoClosingHours"];