I'm looking to extract values from this JSON based on key without any JSON libraries. I figure it can be done in regex, just don't know how. All values are integers.
{"key1":11,"key2":2877,"key3":666,"key4":2906}
I want to return for example, the integer 11, if I give key1 as the input to my method.
public String valueFromKey(String key, String json) {
String result = null;
String patternStr= "Some regex with " + key;
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(json);
while (matcher.find())
result = matcher.group(1);
}
return result;
}
// elsewhere..
String numStr = valueFromKey("key1", "{\"key1\":11,\"key2\":2877,\"key3\":666,\"key4\":2906}");
if (numStr != null)
int val = Integer.parseInt(numStr);