I have a string in json format and I want to extract certain values from that json. For example:
{"foo":"this is foo", "bar":{"nested_bar": "this is nested bar"} }
A user might want to print either foo or bar or both.
Right now, I have a simple class which can read only flat json.
How do I modify the following code to incorporate nested json?
Another question is what is a good way to represent the tags which I want to extract as in flat json? I was passing an array.
public class JsonParser {
public static String[] tagsToExtract;
public JsonParser(String[] tags){
tagsToExtract = tags;
}
public HashMap<String, Object> extractInformation(Text line) throws JSONException{
HashMap<String, Object> outputHash = new HashMap<String, Object>();
JSONObject jsn = new JSONObject(line.toString());
for (int i =0; i < tagsToExtract.length; i++){
outputHash.put(tagsToExtract[i],jsn.get(tagsToExtract[i].toString()));
}
return outputHash;
}
}