2017-05-27 43 views
1

以下是我從Web服務返回的JSON數據的示例。無法反序列化包含元數據和記錄的當前JSON數組

它具有「odata.metadata」的單個節點,「value」的另一個單節點,然後是由3個字段「Code」,「Name」和「XTag」組成的重複記錄。這是被稱爲內容

{"odata.metadata":"https://notthereal.domain:3148/system/OData/$metadata#pageLocationList","value":[{"Code":"LOC-A","Name":"Location A","XTag":"36;DgAAAAJ7/zEAMAAwAC0ATQBBAEkATgAAAAAA8;264943250;"},{"Code":"LOC-B","Name":"Location B","XTag":"36;DgAAAAJ7/zEAMAAxAC0ATQBBAEkATgAAAAAA8;388906690;"},{"Code":"LOC-C","Name":"Location C","XTag":"36;DgAAAAJ7/zEAMAAyAC0ATQBBAEkATgAAAAAA8;388844480;"},{"Code":"LOC-D","Name":"Location D","XTag":"36;DgAAAAJ7/zEAMAAzAC0ATQBBAEkATgAAAAAA8;388876720;"}]} 

我已經在爲模型,我用JSON數據粘貼到類以下的字符串變量。

public class ModelPageLocationList 
{ 
    public string odatametadata { get; set; } 
    public List<Value> value { get; set; } 
} 

public class Value 
{ 
    public string Code { get; set; } 

    public string Name { get; set; } 

    public string XTag { get; set; } 
} 

,這是試圖deserialise它的代碼...

DataPageLocationList = 
JsonConvert.DeserializeObject<List<ModelPageLocationList>>(content); 

..和這樣的錯誤消息。我卡住了!有任何想法嗎?我認爲問題出現在開始時的2個單個記錄的周圍,然後是重複的數據。

ERROR Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. 

這裏是我打電話

public async Task<List<ModelPageLocationList>> RefreshPageLocationListAsync() 
    { 
     DataPageLocationList = new List<ModelPageLocationList>(); 

     var uri = new Uri(string.Format(WebServiceSettings.WebServiceUrl, string.Empty)); 

     try 
     { 
      var response = await client.GetAsync(uri); 
      if (response.IsSuccessStatusCode) 
      { 
       var content = await response.Content.ReadAsStringAsync(); 
       Debug.WriteLine("{0}", content.ToString()); 

       DataPageLocationList = JsonConvert.DeserializeObject<List<ModelPageLocationList>>(content); 

      } 
     } catch (Exception ex) 
     { 
      Debug.WriteLine(@"ERROR {0}", ex.Message); 
     } 

     return (DataPageLocationList); 
    } 
+0

你可以分享你試圖反序列化的JSON嗎? –

+0

這是帖子的第一行 –

回答

0

你需要序列化到你的根對象ModelPageLocationList,而不是你的根對象列表。你的JSON是一個不是對象數組的對象。這將修復它:

DataPageLocationList = 
    JsonConvert.DeserializeObject<ModelPageLocationList>(content); 
+0

我收到一個錯誤「不能隱式地將類型'App.ModelPageLocationList'轉換爲'System.Collections.Generic.List ' –

0

沒有看到您嘗試傳入的JSON數據,我只能根據您提供的信息提供兩個可能的答案。

1)確保您的JSON是這樣的:

{ 
    "odatametadata": "SomeDataHere", 
    "value": [{ 
      "code": "Code1", 
      "name": "Name1", 
      "xTag": "XTag1" 
     }, { 
      "code": "Code1", 
      "name": "Name1", 
      "xTag": "XTag1" 
     }, { 
      "code": "Code1", 
      "name": "Name1", 
      "xTag": "XTag1" 
     }, 
    ] 
} 

2這個屬性添加到您的C#類模型

public class ModelPageLocationList 
{ 
    [JsonProperty("odatametadata")] 
    public string odatametadata { get; set; } 
    [JsonProperty("value")] 
    public List<Value> value { get; set; } 
} 

public class Value 
{ 
    [JsonProperty("code")] 
    public string Code { get; set; } 
    [JsonProperty("name")] 
    public string Name { get; set; } 
    [JsonProperty("xTag")] 
    public string XTag { get; set; } 
} 
+0

(這是帖子的第一行,我無法控制收到的數據)。 –

2

兩件事情。使用JsonProperty來處理odata.metadata屬性,因爲屬性名稱中的點(.)將導致語法問題。

public class ModelPageLocationList { 
    [JsonProperty("odata.metadata")] 
    public string odatametadata { get; set; } 
    [JsonProperty("value")] 
    public List<Value> value { get; set; } 
} 
根據所提供的JSON示例數據

接下來,你需要重構調用代碼

public async Task<ModelPageLocationList> RefreshPageLocationListAsync() { 
    try { 
     var uri = new Uri(string.Format(WebServiceSettings.WebServiceUrl, string.Empty)); 
     var response = await client.GetAsync(uri); 
     if (response.IsSuccessStatusCode) { 
      var result = await response.Content.ReadAsAsync<ModelPageLocationList>(); 
      return result; 
     } 
    } catch (Exception ex) { 
     Debug.WriteLine(@"ERROR {0}", ex.Message); 
    } 
    return null; 
} 
+0

我沒有Content.ReadAsAsync作爲選項。 –

+0

這是一個在System.Net.Http.Formatting.dll中找到的擴展方法, – Nkosi

+0

所以在你的解決方案中沒有JsonConvert? –

0

我做了以下把我的代碼在我的文章開始使用JSON數據的工作。不理想,我寧願能夠做到這一點,而無需編輯JSON字符串。

public async Task<List<ModelPageLocationList>> RefreshPageLocationListAsync() 
    { 
     DataPageLocationList = new List<ModelPageLocationList>(); 
     var uri = new Uri(string.Format(WebServiceSettings.WebServiceUrl, string.Empty)); 

     try 
     { 
      var response = await client.GetAsync(uri); 
      if (response.IsSuccessStatusCode) 
      { 
       var content = await response.Content.ReadAsStringAsync(); 
       Debug.WriteLine("BEFORE {0}", content.ToString()); 

       content = content.Substring(content.IndexOf('['), content.IndexOf(']') - content.IndexOf('[') + 1); 
       Debug.WriteLine("AFTER {0}", content.ToString()); 

       DataPageLocationList = JsonConvert.DeserializeObject<List<ModelPageLocationList>>(content); 
      } 
     } 
     catch (Exception ex) 
     { 
      Debug.WriteLine(@"ERROR {0}", ex.Message); 
     } 
     return (DataPageLocationList); 
    } 

和類從JSON數據

public class ModelPageLocationList 
{ 
    public string Code { get; set; } 

    public string Name { get; set; } 

    public string ETag { get; set; } 
} 

謝謝大家對他們的答覆。

相關問題