访问JArray值无效键值:"&领域;. QUOT阵列位置索引预期 [英] Accessed JArray values with invalid key value: "fields". Array position index expected

查看:1926
本文介绍了访问JArray值无效键值:"&领域;. QUOT阵列位置索引预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的监护人API,试图找回故事,但不断收到一个例外。 ,JSON的字符串包含以下JSON,但是我不能访问使用LINQ身体



下面是:



  {
回应:{
地位:OK,
userTier:经批准,
总:1,
的startIndex:1,
的pageSize:10,
当前页:1,
页:1,$ b $ [{
标识:可持续发展的商业/可持续发展 - 融资 - 在那里,旁边开线程,
b排序依据:最新,
结果 sectionId:可持续经营,
sectionName:卫报可持续发展业务,
webPublicationDate:2014-02-13T13:27:00Z,
webTitle :凡其次为可持续金融 - 开线程?,
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,
田:{
体:&下; IMG SRC = \http://hits.theguardian.com/b/ss/guardiangu-api/1/H.20.3/98867?ns=guardian&pageName=Where+next+for +持续+财务%3F + - +开放+主题+文章+ 2043222&放大器;放大器; CH =卫+可持续+商业与放大器; C2 = 461773&放大器;放大器; C4 = MIC%3A +财务+%28GSB%29%2CMIC%3A +卫+可持续+商务%2CPRO%3A +可持续性+%28Guardian +专业%29&放大器;放大器; C3 = theguardian.com&放大器;放大器; C6 =劳拉+ Paddison&放大器;放大器; C7 = 14 - 2月 - 13放大器;放大器; C8 = 2043222&安培;放大器; C9 = Article\WIDTH = \1\HEIGHT = \1\/> ...
}
}]
$} b $ b}

我曾尝试每一件事情,包括这样的:

 字符串故事=(字符串)JA [回应] [成果] [字段] [身体]; 



更新:



 公共部分类故事:的PhoneApplicationPage 
{
字符串URL;
串jsonData;

// HTTP上使用这样的JSON可以通过get异步方法$ B $被retrived b HttpClient的Web客户端=新的HttpClient();

保护覆盖无效的OnNavigatedTo(NavigationEventArgs E)
{
如果(NavigationContext.QueryString.ContainsKey(钥匙))
{
串encodedValue = NavigationContext.QueryString [钥匙];
URL = Uri.UnescapeDataString(encodedValue);
textBox.Text =网址;
guardianPanorama();
}
}
私人异步无效guardianPanorama()
{

{
HttpResponseMessage结果=等待webClient.GetAsync(URL);

//这需要HTTP响应内容,并变成一个字符串
jsonData =等待Result.Content.ReadAsStringAsync();

JObject JA = JObject.Parse(jsonData);

//这对当前比特币价格和格式它因此有小数位
串的故事=(字符串)JA [回应]的正确用量[成果] [ 田] [身体];

//然后,它被添加到文本框
textBox.Text =故事;
}
赶上(例外错误)
{
MessageBox.Show(我们一直与卫API一个错误);
Console.WriteLine(时出现错误:+错误);
}
}
}



例外:




System.ArgumentException:访问的JArray值无效键值:田。阵列位置索引的预期。



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



解决方案

如果您替换

  //这需要电流比特币的价格和格式它因此有小数位
串的故事=(字符串)JA [回应] [成果] [字段]的正确用量[身体];



  //这对当前比特币价格和格式它因此有小数位
串的故事=(字符串)JA [回应] [成果]的正确用量[0] [域] [身体];

这应该为你的榜样工作。请注意您所提供的示例结果后,方括号,这表示应该占您的访问代码的数组。


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.

Here it is:

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

Update:

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);
        }
    }
}

Exception:

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

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

解决方案

If you replace

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

with

// 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值无效键值:&QUOT;&领域;. QUOT阵列位置索引预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆