开云官网优惠活动
3年前 (2023-05-01)
如果您更换
// This takes the current bitcoin price and formats it so there is the correct amount of decimal places string story = (string)ja["response"]["results"]["fields"]["body"];@H_301_5@与
// This takes the current bitcoin price and formats it so there is the correct amount of decimal places string story = (string)ja["response"]["results"][0]["fields"]["body"];@H_301_5@应该适合您的示例。 同题参见请注意,在您提供的示例中,结果后面的方括号表示要在访问代码中说明的数组。
解决方法
我正在使用监护人API尝试检索故事,但一直收到异常。 对照阅读json的字符串包含以下json,但是我无法使用LINQ访问主体。
这里是:
{ "response":{ "status":"ok","userTier":"approved","total":1,"startIndex":1,"pageSize":10,"currentPage":1,"pages":1,"orderBy":"newest","results":[{ "id":"sustainable-business/sustainable-finance-where-next-open-thread","sectionId":"sustainable-business","sectionName":"Guardian Sustainable Business","webPublicationDate":"2014-02-13T13:27:00Z","webTitle":"Where next for sustainable finance? - open thread","webUrl":"http://www.theguardian.com/sustainable-business/sustainable-finance-where-next-open-thread","apiUrl":"http://content.guardianapis.com/sustainable-business/sustainable-finance-where-next-open-thread","fields":{ "body":"..." } }] } }
我已经尝试了一切,包括:
string story = (string)ja["response"]["results"]["fields"]["body"];更新:
public partial class Story : PhoneApplicationPage { string url; string jsonData; // Http used so the json can be retrived via the get async methods HttpClient webClient = new HttpClient ; protected override void OnNavigatedTo(NavigationEventArgs e) { if (NavigationContext.QueryString.ContainsKey("key")) { string encodedValue = NavigationContext.QueryString["key"]; url = Uri.UnescapeDataString(encodedValue); textBox.Text = url; guardianPanorama ; } } private async void guardianPanorama { try { HttpResponseMessage Result = await webClient.GetAsync(url); // This takes the http response content and is turned into a string jsonData = await Result.Content.ReadAsStringAsync ; JObject ja = JObject.Parse(jsonData); // This takes the current bitcoin price and formats it so there is the correct amount of decimal places string story = (string)ja["response"]["results"]["fields"]["body"]; // It then gets added to the textbox textBox.Text = story; } catch (Exception errors) { MessageBox.Show("There has been a error with the Guardian API"); Console.WriteLine("An error occured:" + errors); } } }例外:
System.ArgumentException:使用无效键值“ fields”访问的JArray值。 相关阅读期望数组位置索引。
在Newtonsoft.Json.Linq.JArray.get_Item(Object key)
你可能想看:
标签:string