Reputation: 547
I can't quite seem to work out how I would deserialize this specific array in c# (from a string!) to a class
[
[
"PrimaryContact",
"=",
"Amy R"
],
"and",
[
"SecondaryContact",
"=",
"Steven G"
],
"and",
[
"ThirdContact",
"=",
"Rachel S"
]
]
Specifically it's the middle section that is throwing me, in this case it is "and". It isn't always just three objects, there could an unlimited amount of objects, with an "and" inbetween each of them.
Upvotes: 0
Views: 98
Reputation: 68710
Using Json.NET, parse everything into a JArray
, then filter out tokens at even indexes, and then convert each into a List<string>
.
You'll end up with a collection of List<string>
, where each list contains 3 elements, e.g., "PrimaryContact"
, "="
and "Amy R"
var array = JArray.Parse(json);
IEnumerable<List<string>> result = array.Where((token, index) => index%2 == 0)
.Select(token => token.ToObject<List<string>>());
This worked for me using your input
Upvotes: 1
Reputation: 700572
You could deserialize it into a list of objects:
List<object> items = new JavaScriptSerializer().Deserialize<List<object>>(json);
Now every other object will be an Object[]
(containing objects that are String
), the ones in between String
.
Upvotes: 2