Skip to content

Instantly share code, notes, and snippets.

@PimDeWitte
Last active April 26, 2016 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PimDeWitte/ed6d81202e1c3ddc8da6020431ec2f0b to your computer and use it in GitHub Desktop.
Save PimDeWitte/ed6d81202e1c3ddc8da6020431ec2f0b to your computer and use it in GitHub Desktop.
//Adds double quotes to string values in an invalid JSON object.
class JsonEditor {
public static String recursiveQuotes(string s) {
string[] stringify = new string[]{ "id","name", "mmcId", "currentMatch"}; // we have to add the search terms because strings can contain int values and they are impossible to identify by checking the value.
string searchTerm = null;
foreach(string check in stringify) {
if (s.Contains (check+"=")) { // we also replace = with :, so check+= is confirmation there are untouched strings.
searchTerm = check;
}
}
if (searchTerm != null) {
// find the search term and the value that follows it, then replace the value with string quotes
string searchArea = s.Split(new string[]{searchTerm+"="}, StringSplitOptions.None)[1];
string value = searchArea.Split (',') [0];
if(value.Contains("}")) {
value = searchArea.Split('}')[0];
}
string replaceTerm = searchTerm + "=" + value;
string newTerm = searchTerm + ":\"" + value + "\"";
s = s.Replace (replaceTerm, newTerm);
return recursiveQuotes (s);
} else {
return s.Replace("=",":").Replace("\"[", "[").Replace("]\"", "]");
}
return s;
}
}
// Now you can call:
JsonConvert.DeserializeObject<Party> (JsonEditor.recursiveQuotes(d));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment