0

The JSON string contains a whitespace in the Resource Objects so 'id' is filled, but 'use id' 'userstatus' 'last event' are null.

I tried 3 different ways in my class but none of them works()

  • Underscore
  • With whitespace
  • Without whitespace

My code(c#):

this is the JSON string

{
"resultsList":[  
    {  
      "id":"1",
      "last event":"Mar 20 07:08 AM",
      "use id":"142AD",
      "user status":"offline"
    },
    {  
      "id":"2",
      "last event":"Mar 19 08:07 AM",
      "use id":"1426BD",
      "user status":"offline"
    }
  ]
}

I Deserialize the JSON in Results.cs(form)

string url = "API url"; var json = new WebClient().DownloadString(url); var results = JsonConvert.DeserializeObject<Rootobject>(json);

Sessions.cs(class)

public class Rootobject
{
    public Resultslist[] resultsList { get; set; }
}

public class Resultslist
{
    public int id { get; set; }
    public string last event { get; set; }
    public string use_id { get; set; }
    public string userstatus { get; set; }

}
skaffman
  • 398,947
  • 96
  • 818
  • 769
SBox
  • 61
  • 1
  • 5

3 Answers3

0

The simplest approach in your case would be:

string json = "{\"resultsList\":[{\"id\":\"1\",\"last event\":\"Mar 20 07:08 AM\",\"use id\":\"142AD\",\"user status\":\"offline\"},{\"id\":\"2\",\"last event\":\"Mar 19 08:07 AM\",\"use id\":\"1426BD\",\"user status\":\"offline\"}]}";

dynamic o = JsonConvert.DeserializeObject(json);
var result = o["resultsList"][0]["last event"];
stefankmitph
  • 3,236
  • 2
  • 18
  • 22
0

Try this:

using System;
using Newtonsoft.Json;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var json = @"{""resultsList"":[{ ""id"":""1"",""last_event"":""Mar 20 07:08 AM"",""use_id"":""142AD"",""user_status"":""offline""}, { ""id"":""2"",
      ""last_event"":""Mar 19 08:07 AM"",
      ""use_id"":""1426BD"",
      ""user_status"":""offline""
    }
  ]
}";
            var results = JsonConvert.DeserializeObject<Rootobject>(json);
            for (int i = 0; i < results.resultsList.Length; i++)
            {
                Console.WriteLine("Id:" + results.resultsList[i].id);
                Console.WriteLine("Last Event:" + results.resultsList[i].last_event);
                Console.WriteLine("User ID:" + results.resultsList[i].use_id);
                Console.WriteLine("User Status:" + results.resultsList[i].user_status);
            }
            Console.ReadKey();
        }
    }
    public class Rootobject
    {
        public Resultslist[] resultsList { get; set; }
    }

    public class Resultslist
    {
        public int id { get; set; }
        public string last_event { get; set; }
        public string use_id { get; set; }
        public string user_status { get; set; }

    }
}
malkam
  • 2,337
  • 1
  • 14
  • 17
0

Sessions.cs(class)

I added a JsonProperty and it works.

public class Rootobject
{
    public Resultslist[] resultsList { get; set; }
}

public class Resultslist
{
    public int id { get; set; }
    [JsonProperty("last event")]
    public string lastevent { get; set; }
    [JsonProperty("use id")]
    public string useid { get; set; }
    [JsonProperty("user status")]
    public string userstatus { get; set; }

}
SBox
  • 61
  • 1
  • 5