0

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",

}
]

enter image description here enter image description here

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
ChunkyFresh
  • 65
  • 1
  • 9
  • 1
    those [ and ] are needed, just don't strip them out – Bloodday Jul 26 '19 at 21:38
  • Additionally, that isn't valid JSON (https://jsonlint.com/). There shouldn't be a comma after `Maria"`. It may _work_ but it isn't valid. – mjwills Jul 26 '19 at 22:08
  • `List star = JsonConvert.DeserializedObject>(data)` https://app.quicktype.io/?share=tAVbpVdytnl94Wstd2Sd – mjwills Jul 26 '19 at 22:33

1 Answers1

1

You have to deserialize your data as List<Entry>, not as List<EntryCollection>

var myData = JsonConvert.DeserializeObject<List<Entry>>(jsonString)

The structure you're trying to deserialize - JsonConvert.DeserializedObject<EntryCollection>(data) - would match json that looks like this:

[
  {
    "Entries":
      [
        { 
           "EntryId": "123",
           "NameLast": "Doe",
           "NameFirst": "John"
        }
      ]
    }
]
mjwills
  • 23,389
  • 6
  • 40
  • 63
Sten Petrov
  • 10,943
  • 1
  • 41
  • 61
  • 1
    ohhhh this is nice. Thank you. This puts things in perspective. mjwillis I know you helped me out throughout the whole thing.Thank you so much! I will grant this the check mark if you don't mind. – ChunkyFresh Jul 26 '19 at 23:00