无法隐式转换类型'Newtonsoft.Json.Linq.JObject'

时间:2013-04-15 20:57:22

标签: c# json

那是其中的一天;我设法解决了2个问题,但我不确定如何修复第3个错误。

我需要返回一个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;
}

2 个答案:

答案 0 :(得分:4)

问题是您正在尝试返回JObject,但由于函数的当前签名,编译器假定它需要返回IEnumerable<JObject>

因此,您需要更改函数的签名,而不是期望IEnumerable<JObject>

public static IEnumerable<JObject> GetListOfHotels()

接受JObject代替:

public static JObject GetListOfHotels()

答案 1 :(得分:0)

使用Ext.Direct客户端代理从Sencha ExtJS 4数据存储区调用Ext.Direct for .NET服务器端堆栈时,我遇到了同样的异常。此服务器端堆栈引用Newtonsoft.Json.dll .NET程序集(.NET 4.0)。我的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'
        }
    }
});