I'm trying to parse JSON results from an API call in order to later have an easier time manipulating it for a WPF UI.
I bolded the [] at the beginning and end of the JSON data because that's how the data is coming back after making the call initially (or at least that's what I'm converting into string from var results).
I believe the [] are causing the issue. I tried following this post, but as you can see from my commented code lines in Program script, after using
//List<EntryCollection> star = JsonConvert.DeserializeObject<List<EntryCollection>>(data);
I got a null exception error at Console.WriteLine(start[0]~
Can someone help me with this? To start simply, I'm trying to write in the console the EntryId of the first result from the API call.
Thanks in advance!
Entry Properties
public class Entry
{
public int EntryID { get; set; }
public string NameFirst { get; set; }
public string NameLast { get; set; }
}
List of Entry Results
public class EntryCollection
{
public List<Entry> Entries { get; set; }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StarRezApi;
using Newtonsoft.Json;
using StarrezLibrary;
Main Program
public class Program
{
static void Main(string[] args)
{
StarRezApiClient connection = new StarRezApiClient("BaseUrL", "Username", "Password");
var results = connection.Select("Entry", Criteria.Equals("NameLast", "Rincon Recio"));
//gets a string representation of JSON
string data = JsonConvert.SerializeObject(results, Formatting.Indented);
Console.WriteLine(data);
//Convert JSON string to a series of objects
EntryCollection star = JsonConvert.DeserializedObject<EntryCollection>(data);
//List<EntryCollection> star = JsonConvert.DeserializeObject<List<EntryCollection>>(data);
//Error
Console.WriteLine(star[0].Entries[0].EntryID);
Console.ReadLine();
}
}
}
JSON Data
[
{
"EntryID": "106076",
"NameLast": "rincon recio",
"NameFirst": "maria",
},
{
"EntryID": "106452",
"NameLast": "Rincon Recio",
"NameFirst": "Mario",
},
{
"EntryID": "103830",
"NameLast": "Rincon Recio",
"NameFirst": "Monica",
},
{
"EntryID": "106077",
"NameLast": "rincon recio",
"NameFirst": "monica",
},
{
"EntryID": "75213",
"NameLast": "Rincon Recio",
"NameFirst": "Mario",
}
]
>(data)` https://app.quicktype.io/?share=tAVbpVdytnl94Wstd2Sd