C
C#3y ago
Welles

Fastest way to change all items in a nested list

Hi all, So atm I serializing following json string into a list:
[
{
"Tag":"Tag1",
"Categories":["A","B"]
},
{
"Tag":"Tag2",
"Categories":["A","B"]
}
]
[
{
"Tag":"Tag1",
"Categories":["A","B"]
},
{
"Tag":"Tag2",
"Categories":["A","B"]
}
]
What would the fastest way be to change all category string to lower case? I'm using a nested for loop now after serialization. Best regards!
4 Replies
TheBoxyBear
TheBoxyBear3y ago
Unless you're willing to make your own json serialization code form scratch, not much you can do besides reading and rewriting the whole thing. Newtonsoft json, you can skip part of the process by making a custom converter that parses the json to a json array, get the tokens you want, modifiy then and convert back into tokens before reserializing the whole thing
ero
ero3y ago
is the goal just to rewrite the file? in that case Regex.Replace or actually just string.Replace
TheBoxyBear
TheBoxyBear3y ago
Could be done with regex but not easily and you have no guaranteed if the file is intended or inline
Welles
WellesOP3y ago
Hi there, regex was an option. The jsonstring will always be single line. However not sure there is much performance gain using regex over for loops. I just don’t like to see nested for loops, but in machine code for loops are actually pretty fast, so this might just be the best solution. Thanks!
Want results from more Discord servers?
Add your server