<legend id='QbVwi'><style id='QbVwi'><dir id='QbVwi'><q id='QbVwi'></q></dir></style></legend>
<tfoot id='QbVwi'></tfoot>
    • <bdo id='QbVwi'></bdo><ul id='QbVwi'></ul>

      <small id='QbVwi'></small><noframes id='QbVwi'>

        <i id='QbVwi'><tr id='QbVwi'><dt id='QbVwi'><q id='QbVwi'><span id='QbVwi'><b id='QbVwi'><form id='QbVwi'><ins id='QbVwi'></ins><ul id='QbVwi'></ul><sub id='QbVwi'></sub></form><legend id='QbVwi'></legend><bdo id='QbVwi'><pre id='QbVwi'><center id='QbVwi'></center></pre></bdo></b><th id='QbVwi'></th></span></q></dt></tr></i><div id='QbVwi'><tfoot id='QbVwi'></tfoot><dl id='QbVwi'><fieldset id='QbVwi'></fieldset></dl></div>

        使用无效键值访问的 JArray 值:“字段".预期数组位置索引

        时间:2023-05-22
        <legend id='oinaD'><style id='oinaD'><dir id='oinaD'><q id='oinaD'></q></dir></style></legend>

        • <i id='oinaD'><tr id='oinaD'><dt id='oinaD'><q id='oinaD'><span id='oinaD'><b id='oinaD'><form id='oinaD'><ins id='oinaD'></ins><ul id='oinaD'></ul><sub id='oinaD'></sub></form><legend id='oinaD'></legend><bdo id='oinaD'><pre id='oinaD'><center id='oinaD'></center></pre></bdo></b><th id='oinaD'></th></span></q></dt></tr></i><div id='oinaD'><tfoot id='oinaD'></tfoot><dl id='oinaD'><fieldset id='oinaD'></fieldset></dl></div>

                <tbody id='oinaD'></tbody>
                • <bdo id='oinaD'></bdo><ul id='oinaD'></ul>
                  <tfoot id='oinaD'></tfoot>

                  <small id='oinaD'></small><noframes id='oinaD'>

                  本文介绍了使用无效键值访问的 JArray 值:“字段".预期数组位置索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在使用 Guardian API 来尝试检索故事,但一直收到异常.json 字符串包含以下 json,但是我无法使用 LINQ 访问正文.

                  I am using the guardian API to try and retrieve stories but keep receiving an exception. The string of json contains the below json, however I can't access the body using 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":"<img src="http://hits.theguardian.com/b/ss/guardiangu-api/1/H.20.3/98867?ns=guardian&amp;pageName=Where+next+for+sustainable+finance%3F+-+open+thread+Article+2043222&amp;ch=Guardian+Sustainable+Business&amp;c2=461773&amp;c4=MIC%3A+Finance+%28GSB%29%2CMIC%3A+Guardian+Sustainable+Business%2CPRO%3A+Sustainability+%28Guardian+Professional%29&amp;c3=theguardian.com&amp;c6=Laura+Paddison&amp;c7=14-Feb-13&amp;c8=2043222&amp;c9=Article" width="1" height="1" />..."
                        }
                       }]
                       }
                  }
                  

                  我已经尝试了所有方法,包括:

                  I have tried every thing including this:

                  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:使用无效键值访问的 JArray 值:字段".应为数组位置索引.

                  System.ArgumentException: Accessed JArray values with invalid key value: "fields". Array position index expected.

                  在 Newtonsoft.Json.Linq.JArray.get_Item(对象键)

                  at Newtonsoft.Json.Linq.JArray.get_Item(Object key)

                  推荐答案

                  如果替换

                  // 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"];
                  

                  // 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"];
                  

                  这应该适用于您的示例.请注意您提供的示例中结果后面的方括号,它表示应该在您的访问代码中考虑的数组.

                  that should work for your example. Note the square bracket after results in the example you provided, that indicates an array that should be accounted for in your access code.

                  这篇关于使用无效键值访问的 JArray 值:“字段".预期数组位置索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 JSON.NET 库在 JArray 中查找节点 (JObject) 下一篇:Json.net 仅序列化某些属性

                  相关文章

                • <i id='LrTvP'><tr id='LrTvP'><dt id='LrTvP'><q id='LrTvP'><span id='LrTvP'><b id='LrTvP'><form id='LrTvP'><ins id='LrTvP'></ins><ul id='LrTvP'></ul><sub id='LrTvP'></sub></form><legend id='LrTvP'></legend><bdo id='LrTvP'><pre id='LrTvP'><center id='LrTvP'></center></pre></bdo></b><th id='LrTvP'></th></span></q></dt></tr></i><div id='LrTvP'><tfoot id='LrTvP'></tfoot><dl id='LrTvP'><fieldset id='LrTvP'></fieldset></dl></div>

                  <small id='LrTvP'></small><noframes id='LrTvP'>

                    1. <legend id='LrTvP'><style id='LrTvP'><dir id='LrTvP'><q id='LrTvP'></q></dir></style></legend>
                      • <bdo id='LrTvP'></bdo><ul id='LrTvP'></ul>
                      <tfoot id='LrTvP'></tfoot>