I have a json file which is somthing like
{ "44451":["67","188","188E","188R","982E","301"]}
How do I read the strings inside of "44451
" and display it in a StringBuilder
in android studio?
Help would be appreciated. Thanks.
I have a json file which is somthing like
{ "44451":["67","188","188E","188R","982E","301"]}
How do I read the strings inside of "44451
" and display it in a StringBuilder
in android studio?
Help would be appreciated. Thanks.
JSONObject json = new JSONObject(jsonString);
JSONArray jArray = json.getJSONArray("44451");
StringBuilder sb;
for(int i=0; i<jArray.length(); i++){
sb.append(jArray.getJSONObject(i).toString());
}
Don't really understand the StringBuilder aspect, but if you want to read the whole text from the JSON, then you can do this.
the whole text is an Object Because it surrounded with { }
, the you have an key 44451
assign to an array ["67",...]
.
to read this, you will read the object first then read the content of the array.
JSONObject object = new JSONObject('');
JSONArray array = object.getJSONArray("44451");
for (int i = 0; i < array.length(); i++) {
String value = array[i];
// other opetations as desired..
}
I would recommend to use FasterXML Jackson library because you can parse JSON into arbitrary user defined types. You could add it a gradle dependency like this
compile 'com.fasterxml.jackson.core:jackson-databind:2.6.2'
compile 'com.fasterxml.jackson.core:jackson-core:2.6.2'
Then you can parse your JSON like this.
String json = "{ \"44451\":[\"67\",\"188\",\"188E\",\"188R\",\"982E\",\"301\"]}";
ObjectMapper mapper = new ObjectMapper();
Map<String, List<String>> values = mapper.readValue(json, new TypeReference<Map<String, List<String>>>() {
});
StringBuilder str = new StringBuilder();
for (String value : values.get("44451")) {
// do whatever you want
}