当前位置: 首页 > 面试题库 >

使用无效的键值“ fields”访问的JArray值。预期的阵列位置索引

曹沛
2023-03-14
问题内容

我正在使用监护人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":"<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\" />..."
      }
     }]
     }
}

我已经尝试了一切,包括:

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)


问题答案:

如果您更换

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

应该适合您的示例。请注意,在您提供的示例中,结果后面的方括号表示要在访问代码中说明的数组。



 类似资料:
  • 我对Java很陌生,只是有点纠结于数组。我有一个代码块,我已经写了当遵循教程,但正在努力理解它,希望有人能解释给我。 null 对不起,如果这是一个琐碎的问题给你,但这是最好的地方,我可以想到转向。

  • 如何使用动态获取和?经过广泛的搜索,我找到了使用管道的想法,但我不知道如何去做。在Angular2中是否有任何内置管道可以做同样的操作?

  • 问题内容: 我已经从事Java项目一年了。我的代码已经工作了好几个月了。几天前,我在Mac(Snow Leopard 10.6.8)上将Java SDK升级到最新版本1.6.0_26。升级后,发生了一些非常奇怪的事情。当我运行某些类时,出现此错误: 位置0x202 rip = 0x202的无效内存访问 但是,如果我使用- Xint(已解释)运行它们,它们会工作,但速度较慢,但​​效果很好。我在使用

  • 附注。Link1和link2是我第二个问题的相关答案。不过,他们并没有回答我的问题。

  • 问题内容: 嗨,我有一个LinkedHashMap(称为信息),其中包含名称/年龄(字符串/整数)对。我想找出,如果我输入键,如何获得键/值的位置。例如,如果我的LinkedHashMap看起来像这样{bob = 12,jeremy = 42,carly = 21}并且我要搜索jeremy,它应该返回1作为其位置1。我希望我可以使用诸如info.getIndex这样的东西(“杰里米”) 问题答案:

  • 我有一个JsonNode,如下所示 我试图得到粉红色的价值,例如这样 但这不起作用——有没有其他方法可以通过Java访问这些值?