Can anyone help me with this json error in c#

Asked

Viewed 77 times

-2

Newtonsoft.Json.Jsonserializationexception: 'Not possible deserialize the current JSON matrix (e.g., [1,2,3]) in type' Json.Pulse 'because the type requires a JSON object (e.g., {"name": "value"}) to deserialize correctly.

class Program
{
    int i1, i2, i3;
    static void Main(string[] args)
    {
        string aux = @"https://api.monday.com:443/v1/boards/251379198/pulses.json?page=7&per_page=25&api_key=39eef5804de940f37d69b3d750c81212";
        var requisicaoWeb = WebRequest.CreateHttp(aux);
        requisicaoWeb.Method = "GET";
        requisicaoWeb.UserAgent = "RequisicaoWebDemo";
        var resposta = requisicaoWeb.GetResponse();
        var streamDados = resposta.GetResponseStream();
        StreamReader reader = new StreamReader(streamDados);
        object objResponse = reader.ReadToEnd();
        var post = JsonConvert.DeserializeObject<pulse>``(objResponse.ToString());

        Console.WriteLine(post);
        Console.ReadLine();
    }
}
public class pulse
{
    public string id;
}
  • Dude, if you can, transcribe your code to the question

  • Ready I made.

1 answer

0

As the error itself points out, you are trying to deserialize an array to an object.

Use: var post = JsonConvert.DeserializeObject<RootObject[]>(objResponse.ToString());

Another thing is that your class pulse is different from the return of Json. I suggest using the http://json2csharp.com/ to map the return of Json with your class pulse.

At most this should work for you.

class Program
{
        int i1, i2, i3;
        static void Main(string[] args)
        {
           string aux = @"https://api.monday.com:443/v1/boards/251379198/pulses.json?page=7&per_page=25&api_key=39eef5804de940f37d69b3d750c81212";
           var requisicaoWeb = WebRequest.CreateHttp(aux);
           requisicaoWeb.Method = "GET";
           requisicaoWeb.UserAgent = "RequisicaoWebDemo";
           var resposta = requisicaoWeb.GetResponse();
           var streamDados = resposta.GetResponseStream();
           StreamReader reader = new StreamReader(streamDados);
           object objResponse = reader.ReadToEnd();
           var post = JsonConvert.DeserializeObject<RootObject[]>(objResponse.ToString());
           foreach(var item in post)
           {
               Console.WriteLine(item.Pulse.id);
           }

           Console.ReadLine();
      }
}
public class Pulse
{
      public string id { get; set; }
}

public class RootObject
{
      public Pulse Pulse { get; set; }
}

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.