I need to be able to enumerate through ANY kind of JSON, multi-dimensional, one-dimensional, nested, e.t.c and be able to search through BOTH Key, Values e.t.c for <TECH>
(.Contains not ==) and replace it with I'm Now Here! "\(^.^)/"
Example:
{"hi":"hello","hi<TECH>":'Lol<TECH>,,,'}
Result should be:
{"hi":"hello","hiI'm Now Here! \"\\(^.^)\/\"":'LolI\'m Now Here! "\\(^.^)\/",,,'}
Im essentially trying to be able to insert data where <TECH>
is while properly taking care of \" \ and \' (and others like this).
Again it may not be a single dimensional line it could be nested. Im trying to JObject.Parse() which allows me to Enumerate through KeyValuePairs but how im meant to send back an edit to the Key/Value im unsure of.
(Also ordering and such is important, I need it to end up being completely the same as the original JSON, formatting and even how its formatted as in one-lined or not has to stay the same)
What I have at the moment
JObject j = JObject.Parse(postData);
foreach (KeyValuePair<string, JToken> item in new JObject(j)) {
string OriginalKey = item.Key.ToString();
string OriginalValue = item.Value.ToString();
string techWord1 = "Other Word's Stuff \":P";
string techWord2 = "I'm Now Here! \"\\(^.^)/\"";
foreach (string techWord in new string[] { "TECH1", "TECH2" }) {
if (OriginalKey.Contains("<" + techWord + ">")) {
j[OriginalKey.Replace("<" + techWord + ">", techWord == "TECH1" ? techWord1 : techWord2)] = j[item.Key];
j.Remove(item.Key);
}
if(OriginalValue.Contains("<" + techWord + ">")) {
j[item.Key] = OriginalValue.Replace("<" + techWord + ">", techWord == "TECH1" ? techWord1 : techWord2);
}
}
}
return j.ToString(Formatting.None);
Issues:
- Does not follow the same exact spacing, indenting e.t.c as the original
- Don't think it works on nested/multi-dimensional JSON
- Don't think every kind of JObject is a KeyValuePair Format? For example how is it going to handle:
[{"hi":"value"}, {"hi2":"value2"}]
Other than those, this seems like a good option. It properly parses all kinds of EOF errors that could occur in both Key:Value scenarios. Just need some way to make its .ToString format EXACTLY like the input JSON, for example lets say there were 2 spaces between { and "hi" in the input JSON, the .ToString() would remove that but I need it to KEEP those.