Home > OS >  Newtonsoft Unexpected character while parsing value
Newtonsoft Unexpected character while parsing value

Time:05-08

I have the problem that I always get this error when deserializing an object. But when I use the bracketed code it works, but not when I retrieve it from my api

Unexpected character encountered while parsing value: S. Path '', line 0, position 0.

 public static async Task<string> GetData()
    {

      
        string data = await API.GetRequest("...");
        Trace.WriteLine(job_data);
        return data;
    }


 public Jobs()
    {


        //var JsonString = @"{'status':true,'data':[{'id':1,'user_id':1}]}";
      
        Trace.WriteLine(GetData().ToString());
        var JsonString = GetData().ToString();
        Model.Root DeserializedClass = JsonConvert.DeserializeObject<Model.Root>(JsonString);
       
        Trace.WriteLine(DeserializedClass);  
        
        
        InitializeComponent();
        
        JobListBox.DataContext = DeserializedClass.Data;
        ContentControl.Content = DeserializedClass.Data;
        
    }
    }

CodePudding user response:

The GetData Function is Async so you should await for it.

    var JsonString = GetData().ToString();

should be

        var JsonString = await GetData();
  • Related