Branden
Branden

Reputation: 237

How would I deserialize a json array that contains pairs without keys?

Let's say I have some json similar to the following:

"productdetails": [["loading"], ["loaded"], ["detailkey", "detailvalue"]]

The formatting is out of my control, and I need to be able to access the 'loading' and 'loaded' parts to make sure the data was loaded properly before moving on further to continue work with the data. I haven't been able to figure out how to setup nested properties that also have no key.

Edit: Should have noted, 'loading' and 'loaded' can be different things and there can be varying amounts of status update arrays before the details or other messages. So, the above could be returned, or something like this:

"productdetails": [["loading"],["empty"],["created"],["loaded"],["detailkey", "detailvalue"]]

Edit 2: Excuse the errors in my json syntax, the colons have been switched out to create a properly syntaxed example.

Upvotes: 0

Views: 77

Answers (1)

HaukurHaf
HaukurHaf

Reputation: 13816

This is just a two-dimensional array, which can be represented as a list of list of string

You should be able to deserialize it to this object:

public class RootObject
{
    public List<List<string>> productdetails { get; set; }
}

I recomment Newtonsoft's Json.Net parser.

Example:

string jsonString = "{\"productdetails\": [[\"loading\"], [\"loaded\"], [\"detailkey\", \"detailvalue\"]]}";
RootObject obj = JsonConvert.Deserialize<RootObject>(jsonString);

Upvotes: 3

Related Questions