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

3
  • 1
    those [ and ] are needed, just don't strip them out Commented Jul 26, 2019 at 21:38
  • Additionally, that isn't valid JSON (jsonlint.com). There shouldn't be a comma after Maria". It may work but it isn't valid. Commented Jul 26, 2019 at 22:08
  • List<Entry> star = JsonConvert.DeserializedObject<List<Entry>>(data) app.quicktype.io/?share=tAVbpVdytnl94Wstd2Sd Commented Jul 26, 2019 at 22:33

1 Answer 1

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"
        }
      ]
    }
]
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.