Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

json

c#

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;
}
like image 568
CareerChange Avatar asked Apr 15 '13 20:04

CareerChange


1 Answers

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()
like image 95
eandersson Avatar answered Sep 30 '22 06:09

eandersson