I'm trying to build a data structure in C# to hold dynamic data created in JavaScript and represented with JSON. I'm looking for advice on how to design the C# data structure to hold the JSON data in a way that makes it easy to search. I'm currently using .NET 4.0; 4.5 is a ways off for this particular project.
The data have the following properties:
true
, false
, some string values, or a nested collection of key-value pairs Here's an example of the JavaScript data:
{
foo: true,
bar: false,
baz: {
a: true,
b: false,
c: "maybe"
}
quux: "maybe"
}
I want to work with the data in the following ways:
foo
, bar
and baz
, but quux
is new to me and I don't know ahead of time that it exists, if it exists, and if it will have a flat or nested value. true
, false
and a number of 'maybe' string
values for 'flat' members. If necessary, I can convert this down to real boolean values (treating the 'maybe' values as true
), but I'd prefer to preserve the 'maybe' values. I think I can know all the 'maybe' values up front, so I could also build an Enum
holding those values, and also throw true
and false
in there so I have a single type for the value. Since the keys are unique, I'm attracted to a Dictionary for speed of lookup and ease of use. But I'm a bit stumped on representing nested collections alongside flat values. This is ridiculously easy in a dynamic language like JavaScript; what's the corresponding idiom in C#?
The real corresponding structure in C# would be an anonymous object/class. I personally would prefer using a Dictionary, either using it with object as value type (then, check if the value for a key is another Dictionary, so you know if you have a nested collection or not) or use a wrapper-class, which holds a Dictionary or a flat value and has some fields like IsNestedCollection and whatever you need.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.