2014-01-06 39 views
0

我想從以下網址https://www.gov.uk/api/foreign-travel-advice.json對象引用不設置到對象的實例

我已經使用http://json2csharp.com/提取類獲取數據,但我不能讓我之後的數據。

任何人誰比我更有經驗請看看,讓我知道我要去哪裏錯了。

這是投擲空全球化志願服務青年

var data = (from j in o["countries"].Children() 
         select new Country 
         { 
           Id  = (string)j["id"], 
           WebUrl = (string)j["name"] 
         }); 

全碼

namespace Web.Domain.FCO.Countries 
{ 
    public class Details 
    { 
     public string Description { get; set; } 
     public object ShortDescription { get; set; } 
     public string Type { get; set; } 
    } 

    public class ContentWithTag 
    { 
     public string Id { get; set; } 
     public string WebUrl { get; set; } 
    } 

    public class Details2 
    { 
     public string Description { get; set; } 
     public object ShortDescription { get; set; } 
     public string Type { get; set; } 
    } 

    public class ContentWithTag2 
    { 
     public string Id { get; set; } 
     public string WebUrl { get; set; } 
    } 

    public class Parent 
    { 
     public string Id { get; set; } 
     public object WebUrl { get; set; } 
     public string Title { get; set; } 
     public Details2 Details { get; set; } 
     public ContentWithTag2 ContentWithTag { get; set; } 
     public object parent { get; set; } 
    } 

    public class Tag 
    { 
     public string Id { get; set; } 
     public object WebUrl { get; set; } 
     public string Title { get; set; } 
     public Details Details { get; set; } 
     public ContentWithTag ContentWithTag { get; set; } 
     public Parent Parent { get; set; } 
    } 

    public class Related 
    { 
     public string Id { get; set; } 
     public string WebUrl { get; set; } 
     public string Title { get; set; } 
     public string Format { get; set; } 
     public string UpdatedAt { get; set; } 
     public string Group { get; set; } 
    } 

    public class Country 
    { 
     public string Id { get; set; } 
     public string Name { get; set; } 
     public string Identifier { get; set; } 
     public string WebUrl { get; set; } 
     public string UpdatedAt { get; set; } 
     public string ChangeDescription { get; set; } 
     public List<object> Synonyms { get; set; } 
    } 

    public class Details3 
    { 
     public string NeedId { get; set; } 
     public bool BusinessProposition { get; set; } 
     public string Description { get; set; } 
     public string Language { get; set; } 
     public bool NeedExtendedFont { get; set; } 
     public List<Country> Countries { get; set; } 
    } 

    public class ResponseInfo 
    { 
     public string Status { get; set; } 
    } 

    public class FcoFullDetails 
    { 
     public string Id { get; set; } 
     public string WebUrl { get; set; } 
     public string Title { get; set; } 
     public string Format { get; set; } 
     public string UpdatedAt { get; set; } 
     public List<Tag> Tags { get; set; } 
     public List<Related> Related { get; set; } 
     public Details3 Details { get; set; } 
     public List<object> RelatedExternalLinks { get; set; } 
     public ResponseInfo ResponseInfo { get; set; } 
    } 

    public class IdAndUrl 
    { 
     public string Id { get; set; } 
     public string WebUrl { get; set; } 
    } 

    public class DisplayFcoCountryList : IDisplayFcoCountryList 
    { 
     public IEnumerable<Country> ListOfCountries() 
     { 
      var doc = FcoCountriesAdvice.GetFcoCountryLinks().Result; 
      // var jd = new [] {JsonConvert.DeserializeObject(doc)}; 
      var jd = JsonConvert.DeserializeObject(doc); 
      JObject o = JObject.Parse(jd.ToString()); 

      //var data = (from j in o["countries"] 
         var data = (from j in o["countries"].Children() 
         select new Country 
         { 
           Id  = (string)j["id"], 
           WebUrl = (string)j["name"] 
         }); 
      return data; 
     } 
    } 
    public static class FcoCountriesAdvice 
    { 
     private static readonly string Key = ConfigurationManager.AppSettings["FCO_AdviceURL"]; 

     public static async Task<string> GetFcoCountryLinks() 
     { 
      using (var ta = new HttpClient()) 
      { 
       const string cacheDataTravelAdvice = "FCOCountryLinks"; 

       ObjectCache travelCache = MemoryCache.Default; 
       var objectInCache = travelCache.Get(cacheDataTravelAdvice) as string; 

       if (objectInCache != null) 
       { 
        return objectInCache; 
       } 

       var url = await ta.GetStringAsync(string.Format("{0}", Key)).ConfigureAwait(false); 

       var policy = new CacheItemPolicy { AbsoluteExpiration = DateTime.Now.AddMinutes(15) }; 

       travelCache.Add(cacheDataTravelAdvice, url, policy); 

       return url; 
      } 
     } 
    } 
} 
+1

您應該使用詳細信息/國家, //jsonviewer.stack.hu/並粘貼到JSON裏面,你可以看到結構 – Alessio

+0

嗨Alessio,謝謝你的鏈接,我看錯結構,應該是var data =(來自j中的o [「details」 ] [「國家」]。兒童()您的鏈接有助於解決問題。 –

回答

0

一個nullref例外,同時創造一個LINQ聲明將最有可能意味着部分代碼:o["countries"].Children()返回NULL,因此不能被迭代。嘗試使用一些簡單的語句對其進行調試時會發生什麼情況:

Console.WriteLine(o [「countries」]);

Console.WriteLine(o [「countries」]。Children()); ();(); Children()。Count());();}};

0

嘗試使用json解串器如JSON.net。這確實所有的JSON-C#轉換爲你:

//make request using the HttpClient class 
    var client = new HttpClient{BaseAddress = new Uri("https://www.gov.uk/api/")}; 
    var response = client.GetAsync("foreign-travel-advice.json").Result; 

    //use Json.Net to convert json to c# classes using generics 
    var content = JsonConvert.DeserializeObject<RootObject> (response.Content.ReadAsStringAsync().Result); 
0

阿萊西奧回答解決了這一問題,參觀他提供的鏈接後,我注意到,我應該一直在使用VAR數據=(從j鄰[「細節「)[」countries「]。兒童()不是var data =(來自j中的o [」國家「]兒童()。問題現在已修復

相關問題