Husseiny
Husseiny

Reputation: 45

Error CS0266 Cannot implicitly convert type 'Newtonsoft.Json.Linq.JObject' to 'string'. An explicit conversion exists (are you missing a cast?)

I want to Return A json from a Method. But i appear to be missing something

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace RESTBrowserGET
{
    public class BrowseRESTGet
    {
        public static string GETRESTSimulator(string restUrl, string reqMethod)
        {
            var request = (HttpWebRequest)WebRequest.Create(restUrl);
            var response = (HttpWebResponse)request.GetResponse();

            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            //return responseString;
            var jsonObj = JsonConvert.DeserializeObject<jsonObj>(responseString);
            return jsonObj;
        }
    }
}

Thats my code. Now apparently i am missing something. Please what do I appear to be missing? I am getting this Error :

Error   CS0266  Cannot implicitly convert type 'Newtonsoft.Json.Linq.JObject' to 'string'. An explicit conversion exists (are you missing a cast?)

There appear to be something i am missing? Please i need some form of Clarification

Edit

Code Looks Like this Now

public class BrowseRESTGet
{
    public static JObject GETRESTSimulator(string restUrl, string reqMethod)
    {
        var request = (HttpWebRequest)WebRequest.Create(restUrl);
        var response = (HttpWebResponse)request.GetResponse();

        var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
        //return responseString;
        var jsonObj = JsonConvert.DeserializeObject(responseString);
        return jsonObj;
    }
}

Now i am Fighting with this As errror

Error   CS0266  Cannot implicitly convert type 'object' to 'Newtonsoft.Json.Linq.JObject'

Edit

Code Edited to

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace RESTBrowserGET
{
    public class BrowseRESTGet
    {
        public static JObject GETRESTSimulator(string restUrl, string reqMethod)
        {
            var request = (HttpWebRequest)WebRequest.Create(restUrl);
            var response = (HttpWebResponse)request.GetResponse();

            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            //return responseString;
            var jsonObj = JsonConvert.DeserializeObject<JObject>(responseString);
            return jsonObj;
        }
    }
}

Latest Edit

public static JObject GETRESTSimulator(string restUrl, string reqMethod)
{
    var request = (HttpWebRequest)WebRequest.Create(restUrl);
    var response = (HttpWebResponse)request.GetResponse();

    var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
    return responseString;
}

Upvotes: 0

Views: 5782

Answers (2)

Adam
Adam

Reputation: 11

your method returns a string and in the return instruction you use the variable jsonObj which is a JObject.

You can use the ToString() method on it if it is implemented correctly.

However, I don't understand what you mean by returning a json from a method. Do you want to return a string containing the json ? Or do you want to return an object with properties matching the one in the json ?

Upvotes: 1

Neil
Neil

Reputation: 11889

responseString IS a string, so there is no need to deserialise it.

Further to your response, I would recommend deserialising the data straight away rather than passing around JObjects.

    public static T GETRESTSimulator<T>(string restUrl, string reqMethod)
    {
        var request = (HttpWebRequest)WebRequest.Create(restUrl);
        var response = (HttpWebResponse)request.GetResponse();

        var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
        //return responseString;
        var jsonObj = JsonConvert.DeserializeObject<T>(responseString);
        return jsonObj;
    }

    public void Test()
    {
       MyDto1 response1 = GETRESTSimulator<MyDto1>("url", "POST");

       MyDto2 response2 = GETRESTSimulator<MyDto2>("Url2", "GET");

    }

Upvotes: 1

Related Questions