2014-02-17 41 views
4

我正在使用監護人API來嘗試和檢索故事,但不斷收到異常。 json字符串包含下面的json,但是我不能使用LINQ訪問正文。使用無效鍵值訪問JArray值:「fields」。陣列位置索引預計

這就是:

{ 
    "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:使用無效鍵值訪問JArray值:「fields」。預期陣列位置索引。

在Newtonsoft.Json.Linq.JArray.get_Item(對象鍵)

+0

你可以發佈如何檢索'ja'JSON對象的代碼嗎? –

+1

以及如果您發佈例外。 Thx –

+0

添加,感謝您的幫助隊友 – user3317526

回答

16

如果用

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

應該爲你的工作,例如更換

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

。請注意您提供的示例中的結果之後的方括號,表示應在您的訪問代碼中考慮的數組。

+0

如果第一個響應是數組形式,第二個響應不是數組形式。有什麼方法可以知道響應是否是數組形式? –

+0

@NickKing我認爲你可以簡單地檢查你的[JToken]的類型(http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JToken.htm),並檢查它是否是JArray。 –

+0

我同意,但我有一個複雜的JSON,我想知道我的JSON最內層的JSON對象的類型。例如,在這個問題的json中,我想檢查「result」對象(在「response」中)是否列表。 –