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

4
我需要返回一个JSON对象,但我得到了以下错误:

错误1:无法隐式转换类型'Newtonsoft.Json.Linq.JObject'为'System.Collections.Generic.IEnumerable<Newtonsoft.Json.Linq.JObject>'。存在显式转换(是否缺少强制转换?)

有人可以帮我解决这个错误吗?
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
嗨,I4V,我解决了另外两个帖子中出现的问题,这个问题当时在我的MVC控制器中,直到我将代码移动到一个类中才出现了这个错误。 - CareerChange
职业转换,很高兴知道你解决了你的问题。 - I4V
1
嗨,I4V,没问题。我认为我遇到的错误越多,我学到的就越多,只是有时候希望不要太多。由于健康原因失去工作并决定建立这个网站以来,我觉得我学到了很多,然后我意识到我还有很多需要学习的地方 :-) - CareerChange
2个回答

5
问题在于您尝试返回一个JObject,但由于函数的当前签名,编译器会认为需要返回一个IEnumerable<JObject>
因此,您需要更改函数的签名,从期望一个IEnumerable<JObject>开始:
public static IEnumerable<JObject> GetListOfHotels()

为了接受一个 JObject,可以这样做:
public static JObject GetListOfHotels()

0

当我使用Ext.Direct客户端代理从Sencha ExtJS 4数据存储调用.NET服务器端堆栈时,遇到了相同的异常。这个服务器端堆栈引用了Newtonsoft.Json.dll .NET程序集(.NET 4.0)。当我的Ext.Direct存储器在sorters和groupers属性中传递嵌套对象时,它抛出了这个异常。通过在花括号周围添加方括号来解决此问题。如果您想知道原因,可以在此处下载框架: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'
        }
    }
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接