Forum Libraries, Components, and Plugins


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

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

jesper2800jesper2800 Member

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
        Task<VersionGetClass> VersionGetMethod([Body] String body);


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

        public string Lasernet { get; set; }

        public string API { get; set; }

        public string AvailableClients { get; set; }

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



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.



Best Answer



    @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 );
  • Hi MKVMALAN, thanx for your reply

    It worked perfectly

Sign In or Register to comment.