访问具有无效键的JArray值:“字段”。预期的数组位置索引我正在使用监护人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":"GuardianSustainableBusiness","webPublicationDate":"2014-02-13T13:27:00Z","webTitle":"可持续金融的下一步是什么?-openthread","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":"..."}}]}}我试过包这个里面的所有事情:stringstory=(string)ja["response"][“结果”][“字段”][“正文”];更新:publicpartialclassStory:PhoneApplicationPage{stringurl;字符串jsonData;//使用Http,因此可以通过获取异步方法检索jsonHttpClientwebClient=newHttpClient();保护tedoverridevoidOnNavigatedTo(NavigationEventArgse){if(NavigationContext.QueryString.ContainsKey("key")){stringencodedValue=NavigationContext.QueryString["key"];url=Uri.UnescapeDataString(encodedValue);textBox.Text=url;监护人全景图();}}privateasyncvoidguardianPanorama(){try{HttpResponseMessageResult=awaitwebClient.GetAsync(url);//这会获取http响应内容并将其转换为字符串jsonData=awaitResult.Content.ReadAsStringAsync();JObjectja=JObject.Parse(jsonData);//这采用当前的比特币价格并将其格式化,因此小数位数正确stringstory=(string)ja["response"]["results"]["fields"]["body"];//然后它被添加到文本框textBox.Text=story;}catch(Exceptionerrors){MessageBox.Show("TherehasbeenaerrorwiththeGuardianAPI");Console.WriteLine("发生错误:"+errors);}}}例外:System.ArgumentException:使用无效键值访问JArray值:“fields"预期的数组位置索引。在Newtonsoft.Json.Linq.JArray.get_Item(Objectkey)ja["response"]["results"]["fields"]["body"];same//这会获取当前的比特币价格并对其进行格式化,以便有正确的小数位数stringstory=(string)ja["response"]["results"][0]["fields"]["body"];这应该适用于您的示例。请注意您在提供的示例中得到的方括号,表示访问代码应该中考虑的数组以上是C#学习教程:使用无效键值访问JArray值:"fields"预期的数组位置索引分享了所有内容,如果对大家有用还需要详细了解一下C#学习教程,希望大家多多关注——本文收集自网络,不代表立场,如涉及侵权,请点击右边联系管理员删除。如有转载请注明出处:
