Created
February 12, 2017 06:43
-
-
Save tyler-boyd/0022e9c7132c364bbba18800e684f310 to your computer and use it in GitHub Desktop.
AHH
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
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