2017-01-31 26 views
-1

我想一個FeedResponse轉換成名單,但未能序列化的字符串,它拋出一個錯誤轉換的FeedResponse <Class>到列表<Class>在DocumentDB

無法反序列化當前的JSON對象(例如{「名稱「:」value「})轉換爲類型'System.Collections.Generic.List`1 [Lutran.Api.Models.Infinity]',因爲該類型需要一個JSON數組(例如[1,2,3])來正確地反序列化。 要修復此錯誤,請將JSON更改爲JSON數組(例如[1,2,3])或更改反序列化的類型,以使其爲正常的.NET類型(例如,不是像整數這樣的基本類型,也不是集合類型像數組或列表),可以從JSON對象反序列化。 JsonObjectAttribute也可以添加到類型中,以強制它從JSON對象反序列化。 路徑「令牌」,第1行,位置9

我已用本link和獲取數據返回的整個輸出對象時用於分頁邏輯,但是當即時通訊試圖將其轉換使用fails.Tried一個可枚舉的對象,但它顯示類型轉換錯誤。使用動態對象但無法從中提取ResponseContinuation值。

利用牛頓柔軟JSON轉換(反序列化字符串)

var query = client.CreateDocumentQuery<Document>(collection, options).AsDocumentQuery(); 

if (query.HasMoreResults) 
{ 
    var result = await query.ExecuteNextAsync<LeadDataView>(); 

    objLeadDataView.ResponseContinuation = result.ResponseContinuation; 
    objLeadDataView.InfinityDataView = JsonConvert.DeserializeObject<List<Infinity>>(result.ToString()); 
    response = objLeadDataView; 
} 
+0

你的異常信息本身就已說明,您可以使用_Newtonsoft Json_你的JSON解析到.NET對象 –

+0

請張貼代碼如下[MVCE(http://stackoverflow.com/help/mcve) – peval27

+0

@RahulSingh checck代碼我得到這個錯誤,當我嘗試從newtonsoft只使用jsonconvert –

回答

0

我想通了

public class LeadDataView 
{ 
    public string ResponseContinuation { get; set; } 

    public FeedResponse<Infinity> InfinityDataView { get; set; } 
} 

if (query.HasMoreResults) 
      { 
       var result = await query.ExecuteNextAsync<Infinity>(); 

       objLeadDataView.ResponseContinuation = result.ResponseContinuation; 
       objLeadDataView.InfinityDataView = result; 
       response = objLeadDataView; 
      } 

所以上面的代碼發送下面頂部和無窮級的數據延續標記。 enter image description here

相關問題