CareerChange
CareerChange

Reputation: 669

Cannot implicitly convert type 'Newtonsoft.Json.Linq.JObject'

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

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

Upvotes: 4

Views: 19280

Answers (2)

JustBeingHelpful
JustBeingHelpful

Reputation: 18980

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/.

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

Upvotes: 0

eandersson
eandersson

Reputation: 26352

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.

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

public static IEnumerable<JObject> GetListOfHotels()

To accept a JObject instead:

public static JObject GetListOfHotels()

Upvotes: 5

Related Questions