2013-04-15 124 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' 
     } 
    } 
});