C
C#3y ago
Cyanox

Parsing JSON

I'm struggling to parse and iterate over a JSON array of "key/object" elements. For instance, something in this format:
{
"key1":{
"param1":value,
"param2":value
},
"key2":{
"parm1":value,
"param2":value
}
}
{
"key1":{
"param1":value,
"param2":value
},
"key2":{
"parm1":value,
"param2":value
}
}
Using Netwonsoft for JSON parsing.
30 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Cyanox
CyanoxOP3y ago
There's no code to show I have iteration and such, I'm just struggling on the one line to parse that JSON string into an object Y'know, something along this line var obj = JsonConvert.DeserializeObject<>(input); But I can't figure out what to put as the type for that to parse
Thinker
Thinker3y ago
You need to create a class which models your JSON structure
Cyanox
CyanoxOP3y ago
I didn't want to do that, ideally doing this without the need of that Figured it out anyways, Dictionary<string, dynamic> did the trick
Thinker
Thinker3y ago
Why?
Cyanox
CyanoxOP3y ago
Because why have another class when it's unneeded
Thinker
Thinker3y ago
Do not use dynamic. Ever.
Cyanox
CyanoxOP3y ago
One less thing to worry about Why not?
Thinker
Thinker3y ago
DO NOT USE DYNAMIC
Cyanox
CyanoxOP3y ago
I've never used it before
Thinker
Thinker3y ago
C# is a statically typed language which means that every property of every class is available at compile-time. dynamic just skids around that and says fuck you to the compiler and allows you to access random things at runtime. This might seem great, but it comes at firstly a pretty major performance cost, and secondly the cost of not being able to navigate through your JSON structure safely.
Mayor McCheese
dynamic solves some corner cases, but should be avoided
Cyanox
CyanoxOP3y ago
Gotcha I guess I'll make a class then
Thinker
Thinker3y ago
You are much better off going through the trouble of just creating a class modelling your JSON structure. If you're using VS it even has a tool for this, and there are online tools for generating this.
Mayor McCheese
if you don't make a class you're always going to be tunneling through to get config
Cyanox
CyanoxOP3y ago
I know how, I was just trying to see if there was an alternative
Mayor McCheese
environment variables but you'll not want to use those directly either
Cyanox
CyanoxOP3y ago
Actually, even making a class, I'm not sure how I'd deserialize this Assume that within a key there's potential for there to be more than 1 param I was thinking it'd be like string, obj kinda thing
Mayor McCheese
you're using newtonsoft?
Cyanox
CyanoxOP3y ago
Yeah
Mayor McCheese
var myconfig = JsonConvert.DeserializeObject<MyConfig>(someJsonStriing);
Cyanox
CyanoxOP3y ago
I know that, the MyConfig part is the part I'm struggling on I sent a sample of what I'm trying to parse above
Angius
Angius3y ago
Could be a dictionary of dictionaries, could be a class with dictionaries, could be a dictionary of classes, could be a class with classes Depends on if, and if so which, keys are always the same or dynamic
Cyanox
CyanoxOP3y ago
Keys are dynamic Eh I might just make this an array and put the key in the obj It's kinda pointless doing what I'm doing for my purposes
Angius
Angius3y ago
A dictionary of dictionaries, then Or of classes
Cyanox
CyanoxOP3y ago
I'm just gonna make it an array
Angius
Angius3y ago
It won't deserialize to an array, since you have the keys there At least I don't think it will
Cyanox
CyanoxOP3y ago
I'm changing the json format
Angius
Angius3y ago
Haven't used Newtonsoft in ages, so can't tell
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server