C
C#2y 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 User2y ago
Message Not Public
Sign In & Join Server To View
Cyanox
Cyanox2y 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
Thinker2y ago
You need to create a class which models your JSON structure
Cyanox
Cyanox2y 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
Thinker2y ago
Why?
Cyanox
Cyanox2y ago
Because why have another class when it's unneeded
Thinker
Thinker2y ago
Do not use dynamic. Ever.
Cyanox
Cyanox2y ago
One less thing to worry about Why not?
Thinker
Thinker2y ago
DO NOT USE DYNAMIC
Cyanox
Cyanox2y ago
I've never used it before
Thinker
Thinker2y 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
Cyanox2y ago
Gotcha I guess I'll make a class then
Thinker
Thinker2y 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
Cyanox2y 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
Cyanox2y 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
Cyanox2y ago
Yeah
Mayor McCheese
var myconfig = JsonConvert.DeserializeObject<MyConfig>(someJsonStriing);
Cyanox
Cyanox2y 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
Angius2y 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
Cyanox2y 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
Angius2y ago
A dictionary of dictionaries, then Or of classes
Cyanox
Cyanox2y ago
I'm just gonna make it an array
Angius
Angius2y ago
It won't deserialize to an array, since you have the keys there At least I don't think it will
Cyanox
Cyanox2y ago
I'm changing the json format
Angius
Angius2y ago
Haven't used Newtonsoft in ages, so can't tell
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server
More Posts
Accidentally merged untested code in the stable branch, can't find the merge to revert [Answered]Title explains it. https://github.com/TheBoxyBear/charttools I accidentally merged beta into stable EF Core + SQLite order by decimal value [Answered]Hey I tried to sort a query via a decimal column but I get this exception: ``System.NotSupportedExceRider suggestion - creating a property for a DateTime field will introduce struct copyingI encountered this warning from Rider recently when refactoring some old code. I had a class with seCan someone explain this for me```cs namespace CSharp { internal class Program { static void Main(string[] args) ``HashCode randomly changing on structures.I've never in my career of coding have seen the behavior I'm about to describe so I'm hoping to learCOMException on Windows.Storage.Pickers.FolderPickerI am receiving the error `Error HRESULT E_FAIL has been returned from a call to a COM component` wheLinq Where JsonSerialization not workingHey, is there a way to get the following running: ```cs dbSet.Set<Table>().Where(x => JsonSerializerMicrosoft's DI Hierarchically controlled lifetime manager? [Answered]With Microsoft's DI, what's the equivalent of hierarchically controlled lifetime manager? The situatDisable bootstrap multiselect options after selectingHi. I have a bootstrap `<select multiple/>`. When one is selected, I want to disable all other selecReading ConfigurationSay I have a class like this: ```cs public StepGenerator(MigrationSteps migration) {