不能隐式转换类型'Newtonsoft.Json.Linq.JObject'

时间:2022-09-15 11:58:12

It's been one of those days; I managed to solve 2 problems, but I'm not sure how to fix the 3rd error.

这是其中的一天;我成功地解决了两个问题,但是我不知道如何修正第三个错误。

I need to return a json object, but I am getting the following error:

我需要返回一个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?)

Can anyone help me solve this error?

有人能帮我解决这个错误吗?

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 个解决方案

#1


4  

The problem is that you are trying to return a JObject, but because of the current signature of your function, the compiler assumes that it needs a IEnumerable<JObject> returned.

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

So you would need to change the signature of your function from expecting an IEnumerable<JObject>:

因此,需要将函数的签名从预期的IEnumerable 修改为:

public static IEnumerable<JObject> GetListOfHotels()

To accept a JObject instead:

接受一份工作:

public static JObject GetListOfHotels()

#2


0  

I had the same exception when calling the Ext.Direct for .NET server-side stack from a Sencha ExtJS 4 data store using the Ext.Direct client proxy. This server-side stack references the Newtonsoft.Json.dll .NET assembly (.NET 4.0). My Ext.Direct store was passing nested objects within the sorters and groupers properties in the store when it threw this exception. I fixed it by adding square brackets around the curly braces. If you wish to see why, you can download the framework here: https://code.google.com/p/extdirect4dotnet/.

在使用Ext.Direct客户端代理从Sencha ExtJS 4数据存储调用. net服务器端堆栈时,我遇到了相同的异常。这个服务器端堆栈引用Newtonsoft.Json。dll。net大会。NET 4.0)。当我的Ext.Direct存储抛出这个异常时,它正在存储中的sorter和groupers属性中传递嵌套对象。我在花括号周围加了方括号。如果你想知道为什么,你可以在这里下载这个框架:https://code.google.com/p/extdirect4dotnet/。

Old (throws exception):

老(异常):

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'
        }
    }
});

New (fixed by including brackets):

新(包括括号):

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'
        }
    }
});

#1


4  

The problem is that you are trying to return a JObject, but because of the current signature of your function, the compiler assumes that it needs a IEnumerable<JObject> returned.

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

So you would need to change the signature of your function from expecting an IEnumerable<JObject>:

因此,需要将函数的签名从预期的IEnumerable 修改为:

public static IEnumerable<JObject> GetListOfHotels()

To accept a JObject instead:

接受一份工作:

public static JObject GetListOfHotels()

#2


0  

I had the same exception when calling the Ext.Direct for .NET server-side stack from a Sencha ExtJS 4 data store using the Ext.Direct client proxy. This server-side stack references the Newtonsoft.Json.dll .NET assembly (.NET 4.0). My Ext.Direct store was passing nested objects within the sorters and groupers properties in the store when it threw this exception. I fixed it by adding square brackets around the curly braces. If you wish to see why, you can download the framework here: https://code.google.com/p/extdirect4dotnet/.

在使用Ext.Direct客户端代理从Sencha ExtJS 4数据存储调用. net服务器端堆栈时,我遇到了相同的异常。这个服务器端堆栈引用Newtonsoft.Json。dll。net大会。NET 4.0)。当我的Ext.Direct存储抛出这个异常时,它正在存储中的sorter和groupers属性中传递嵌套对象。我在花括号周围加了方括号。如果你想知道为什么,你可以在这里下载这个框架:https://code.google.com/p/extdirect4dotnet/。

Old (throws exception):

老(异常):

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'
        }
    }
});

New (fixed by including brackets):

新(包括括号):

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'
        }
    }
});