Deserialize json response, in quotes, using Refit/Newtonsoft.json

jesper2800jesper2800 Member

Hi,
I'm new to Xamarin, Refit and Newtonsoft and theproblem is not directly related to Xamarin. It's the first step to an Android/IOS app though.

I've made a little test program to get familiar with Refit. My problem is, that the response I receive from the API, is enclosed in quotes.
Example: "{"Lasernet" : "9.7.103", "API" : "1.0", "AvailableClients" : "0"}"

I Receive the error below, when I run the code.

Newtonsoft.Json.JsonSerializationException: 'Error converting value "{"Lasernet" : "9.7.103", "API" : "1.0", "AvailableClients" : "0"}" to type 'RefitTestConsole.VersionGetClass'. Path '', line 1, position 79.'

Code -----------------------------------------------
using System;
using Newtonsoft.Json;
using Refit;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RefitTestConsole
{
    public interface IVersionGet
    {
        [Post("/webinputport/WebServer1/webservice/versionGet")]
        Task<VersionGetClass> VersionGetMethod([Body] String body);

    }

    public class VersionGetClass
    {
        //public string Key { get; set; }
        //public string Value { get; set; }


        //[JsonProperty("Lasernet")]
        public string Lasernet { get; set; }

        //[JsonProperty("API")]
        public string API { get; set; }

        //[JsonProperty("AvailableClients")]
        public string AvailableClients { get; set; }
    }

class Program
    {
        static async Task Main()
        {
            var VersionGetService = RestService.For<IVersionGet>("http://localhost:9099");
            var VersionResponse = await VersionGetService.VersionGetMethod("{}");

            //Console.WriteLine($"{VersionResponse}");
            //Console.WriteLine($"{VersionResponse.Key}");
            //Console.WriteLine($"{VersionResponse.Value}");
            Console.WriteLine($"{VersionResponse.Lasernet}");
            Console.WriteLine($"{VersionResponse.API}");
            Console.WriteLine($"{VersionResponse.AvailableClients}");

            Console.ReadLine();
        }
    }
}

Code -----------------------------------------------

Is there a way to remove the quotes before the deserialization begins?

I've tried calling other APIs, with success, using the same code. These APIs do not enclose their response in quotes.

I have no control over the response, that's enclosed in quotes.

Thanx

Tagged:

Best Answer

Answers

  • MKVIMALANMKVIMALAN Member ✭✭

    @jesper2800 Hi, If your response is too small as you gave it. You can use JObject
    For an example if your response var locresponse = assign your api response directly

     var response = {"Lasernet" : "9.7.103", "API" : "1.0", "AvailableClients" : "0"}             
    
                    if (response.IsSuccessStatusCode)
                    {
                        result = response.Content.ReadAsStringAsync().Result;
    
                        var _apiresponse = JsonConvert.DeserializeObject<JObject>(result);
    
                        string Lasernet = _apiresponse ["Lasernet"].ToString();
            string API = _apiresponse ["API"].ToString();
                        string AvailableClients = _apiresponse ["AvailableClients"].ToString();
    
                        Debug.WriteLine(Lasernet );
            Debug.WriteLine(API);
            Debug.WriteLine(AvailableClients);
                    }
    
  • jesper2800jesper2800 Member

    Hi MKVMALAN, thanx for your reply

    It worked perfectly

Sign In or Register to comment.