0

I used this solution to deserialize my JSON string. I am trying to avoid third-party libraries if at all possible.

However, the string of JSON I'm working with is structured like so:

{"BatchId":"32","BatchPriority":"NORMAL","Entries":{"Entry":
    [{"EntryId":"185","EntryPriority":"HIGH","Value":"0.0"},
     {"EntryId":"172","EntryPriority":"LOW","Value":"122.0"}]
}}

I am able to deseralize my JSON to set the BatchId and BatchPriority to the Batch object (see below). However, I am unable to set the array of "Entries" from the JSON, whose value appears to be a JSON string itself.

How do I structure my object such that I am able to deseralize and set the array of "Entries" to the Batch object just I am able to set values for the BatchId and BatchPriority properties? I know public Array Entries { get; set; } is wrong but I am not sure how to approach this.


Batch object:

public class Batch
{
    public int BatchId { get; set; }

    public string BatchPriority { get; set; }

    public Array Entries { get; set; } //wrong
}

Entry object:

public class Entry
{
    public int EntryId { get; set; }

    public string EntryPriority { get; set; }

    public double Value { get; set; }
}
Community
  • 1
  • 1

2 Answers2

1

You could create a class that holds a List<Entry>

public class Entries
{
    public List<Entry> Entry { get; set; }
}

If you take a look at Json2csharp, it will generate the classes for you.

Christian Phillips
  • 18,399
  • 8
  • 53
  • 82
0

Your property public Array Entries { get; set; } is a System.Array called Entries, you want an array or collection of Entry classes called Entry e.g.

public class Batch
{
    public int BatchId { get; set; }

    public string BatchPriority { get; set; }

    public Entry[] Entry { get; set; } 
}
Ben Robinson
  • 21,601
  • 5
  • 62
  • 79