Last active
April 26, 2016 13:00
-
-
Save PimDeWitte/ed6d81202e1c3ddc8da6020431ec2f0b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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