System.Text.Json.JsonException: "The JSON value could not be converted to ConsoleApp3.Data. Path: $ : LineNumber: 0 : BytePositionInLine: 1."



  • I inserted a reference to the api-https://api.vimeworld.ru/user/name/" + nickname, and the program swears that you can't transform string into System.Text.Json.JsonSerializerOptions, if the brackets leave empty, it's "Jolet.Jusson value." Path: $ : LineNumber: 0 : BytePositionInLine: 1."

    Class Data

    public class Data
        {
            public int id { get; set; }
            public string username { get; set; }
            public int level { get; set; }
            public float levelPercentage { get; set; }
            public string rank { get; set; }
            public int playedSeconds { get; set; }
            public int lastSeen { get; set; }
            public Guild guild { get; set; }
        }
    

    Code

    class Program
        {
            private static readonly HttpClient client = new HttpClient() { DefaultRequestVersion = HttpVersion.Version20 };
    
        static async Task Main(string[] args)
        {
            
            string nickname = Console.ReadLine();
            string requesUri = "https://api.vimeworld.ru/user/name/" + nickname;
            
            using HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requesUri);
            using HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
            Data result = await response.Content.ReadFromJsonAsync<Data>();
            int id = result.id;
    
            Console.ReadKey();
    

    Text Json - https://api.vimeworld.ru/user/name/xtrafrancyz

    _
    _
    _



  • JSON contains a mass []Not the object {}

    The private case of a single-element solution would look like that.

    Data result = (await response.Content.ReadFromJsonAsync<Data[]>())[0];
    

Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2