2013-04-15 111 views
4

這是那些日子之一;我設法解決了2個問題,但我不知道如何解決第三個錯誤。不能隱式轉換類型'Newtonsoft.Json.Linq.JObject'

我需要返回JSON對象,但我得到以下錯誤:

Error 1 Cannot implicitly convert type 'Newtonsoft.Json.Linq.JObject' to 'System.Collections.Generic.IEnumerable<Newtonsoft.Json.Linq.JObject>'. An explicit conversion exists (are you missing a cast?) 

誰能幫我解決這個錯誤?

public static IEnumerable<JObject> GetListOfHotels() 
{ 
    const string dataPath = "https://api.eancdn.com/ean-services/rs/hotel/v3/list?minorRev=99&cid=55505&apiKey=key&customerUserAgent=Google&customerIpAddress=123.456&locale=en_US&currencyCode=USD&destinationString=washington,united+kingdom&supplierCacheTolerance=MED&arrivalDate=12/12/2013&departureDate=12/15/2013&room1=2&mberOfResults=1&supplierCacheTolerance=MED_ENHANCED"; 
    var request   = WebRequest.Create(dataPath); 
    request.Method  = "POST"; 
    const string postData = dataPath; 
    var byteArray   = Encoding.UTF8.GetBytes(postData); 
    request.ContentType = "application/json"; 
    request.ContentLength = byteArray.Length; 
    var dataStream = request.GetRequestStream(); 
    dataStream.Write(byteArray, 0, byteArray.Length); 
    dataStream.Close(); 

    var response = request.GetResponse(); 
    var responseCode = (((HttpWebResponse) response).StatusDescription); 

    var responseStream = response.GetResponseStream(); 

    var responseReader = new StreamReader(responseStream, Encoding.UTF8); 
    var responseString = responseReader.ReadToEnd(); 

    var root = JObject.Parse(responseString); 

    return root; 
} 
+1

而是問同一個問題多次,我會發布JSON字符串並問*我做錯了什麼?* – I4V

+0

嗨I4V,我解決了問題與其他2個職位我當時是在我的mvc控制器,直到我把代碼移動到類th在這個錯誤發生。 – CareerChange

+0

CareerChange,很高興知道你解決了你的問題。 – I4V

回答

4

的問題是,你試圖返回JObject,而是因爲你的函數的電流簽名,編譯器假定它需要返回IEnumerable<JObject>

所以,你會需要你的函數的簽名,從預期的IEnumerable<JObject>改變:

public static IEnumerable<JObject> GetListOfHotels() 

要接受JObject代替:

public static JObject GetListOfHotels() 
+0

謝謝eandersson,工作 – CareerChange

+0

無後顧之憂。很高興我能幫忙@CareerChange。 :) – eandersson

0

我呼叫分機時有同樣的例外。使用Ext.Direct客戶端代理直接從Sencha ExtJS 4數據存儲中獲得.NET服務器端堆棧。此服務器端堆棧引用Newtonsoft.Json.dll .NET程序集(.NET 4.0)。當引發此異常時,My Ext.Direct商店在商店中的分揀機和分組器屬性內傳遞嵌套對象。我通過在花括號中添加方括號來修復它。如果你想知道爲什麼,你可以在這裏下載框架:https://code.google.com/p/extdirect4dotnet/

老(拋出異常):

Ext.define('MyApp.store.Hierarchy', { 
    extend : 'Ext.data.Store', 
    model : 'R.model.Hierarchy', 
    sorters: { 
     property: 'namespace_id', 
     direction: 'ASC' 
    }, 
    remoteSort: true, 
    groupers: { 
     property: 'namespace_id', 
     direction: 'ASC' 
    }, 
    remoteGroup: true, 
    proxy: {   
     type: 'direct', 
     directFn: Tree_CRUD.read, 
     reader: { 
      root: 'data' 
     } 
    } 
}); 

新(由包括支架固定):

Ext.define('MyApp.store.Hierarchy', { 
    extend : 'Ext.data.Store', 
    model : 'R.model.Hierarchy', 
    sorters: [{ 
     property: 'namespace_id', 
     direction: 'ASC' 
    }], 
    remoteSort: true, 
    groupers: [{ 
     property: 'namespace_id', 
     direction: 'ASC' 
    }], 
    remoteGroup: true,3 
    proxy: {   
     type: 'direct', 
     directFn: Tree_CRUD.read, 
     reader: { 
      root: 'data' 
     } 
    } 
});