Skip to content

Instantly share code, notes, and snippets.

@tyler-boyd
Created February 12, 2017 06:43
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 tyler-boyd/0022e9c7132c364bbba18800e684f310 to your computer and use it in GitHub Desktop.
Save tyler-boyd/0022e9c7132c364bbba18800e684f310 to your computer and use it in GitHub Desktop.
AHH
private HashMap<String, String> parseObject(String jsonObject) {
HashMap<String, String> ret = new HashMap<String, String>();
int arrayDepth = 0, objDepth = 0;
boolean inQuotes = false;
String currentKey = "";
String currentElement = "";
for (int i = 0; i < jsonObject.length(); i++) {
char ci = jsonObject.charAt(i);
if(ci == '"')
inQuotes = !inQuotes;
if(inQuotes) {
currentElement += ci;
continue;
}
if(ci == '{') {
objDepth++;
if(objDepth == 1)
continue;
}
if(ci == '}') {
objDepth--;
}
if(ci == '[') {
arrayDepth++;
}
if(ci == ']') {
arrayDepth--;
}
if(ci == ':' && objDepth == 1) {
currentKey = currentElement;
currentElement = "";
continue;
}
if(ci == ',' && objDepth == 1 && arrayDepth == 0) {
ret.put(currentKey, currentElement);
currentKey = "";
currentElement = "";
continue;
}
if(ci == '}' && objDepth == 0) {
ret.put(currentKey, currentElement);
return ret;
}
if(objDepth == 0 || (ci == ' ' && objDepth == 1 && arrayDepth == 0))
continue;
currentElement += ci;
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment