【问题标题】:Cannot implicitly convert type 'Newtonsoft.Json.Linq.JObject'无法隐式转换类型“Newtonsoft.Json.Linq.JObject”
【发布时间】:2013-04-15 20:57:21
【问题描述】:

我需要返回一个 json 对象,但我收到以下错误:

错误 1 ​​无法隐式转换类型“Newtonsoft.Json.Linq.JObject” 到 'System.Collections.Generic.IEnumerable'。 存在显式转换(您是否缺少演员表?)

谁能帮我解决这个错误?

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

【问题讨论】:

  • 我不会多次问同一个问题,而是发布 json 字符串并问 我做错了什么?
  • 嗨 I4V,我自己解决了其他 2 个帖子的问题,当时我的 mvc 控制器中,直到我将代码移到一个类中才发生此错误。
  • CareerChange,很高兴知道你解决了你的问题。
  • 嗨 I4V 没问题,我认为我遇到的错误越多,我学到的越多,只是有时希望它不是那么多。自从因健康状况不佳而失业并决定建立这个网站以来,我认为我学到了很多东西,然后我意识到我还需要学习多少:-)

标签: c# json


【解决方案1】:

问题是您试图返回JObject,但由于您的函数的当前签名,编译器假定它需要返回IEnumerable&lt;JObject&gt;

因此,您需要将函数的签名从期望 IEnumerable&lt;JObject&gt; 更改:

public static IEnumerable<JObject> GetListOfHotels()

改为接受JObject

public static JObject GetListOfHotels()

【讨论】:

  • 不用担心。很高兴我能帮助@CareerChange。 :)
【解决方案2】:

在使用 Ext.Direct 客户端代理从 Sencha ExtJS 4 数据存储中调用 Ext.Direct for .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'
        }
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多