不能隐式转换'Newtonsoft.Json.Linq.JObject'
本文关键字:Json Linq JObject Newtonsoft 不能 转换 | 更新日期: 2023-09-27 18:09:26
我需要返回一个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¤cyCode=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;
}
问题是您试图返回JObject
,但由于函数的当前签名,编译器假定它需要返回IEnumerable<JObject>
。
因此,您需要更改函数的签名,而不是期望IEnumerable<JObject>
:
public static IEnumerable<JObject> GetListOfHotels()
接受JObject
代替:
public static JObject GetListOfHotels()
当使用Ext.Direct客户端代理从Sencha ExtJS 4数据存储调用。net服务器端堆栈的Ext.Direct时,我也有同样的异常。此服务器端堆栈引用Newtonsoft.Json.dll . net程序集(。NET 4.0)。当我的Ext.Direct存储抛出此异常时,它正在存储中的sorters和groupers属性中传递嵌套对象。我通过在花括号周围添加方括号来修复它。如果你想知道为什么,你可以在这里下载这个框架:https://code.google.com/p/extdirect4dotnet/.
Old(抛出异常):
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'
}
}
});