{
"name1": [
"value1"
],
"name2": [
"value1",
"value2",
"value3",
"value4",
"value5",
],
"name3": [
"value1"
]
}
new to JSON.Please help me to read this name and value in this file
{
"name1": [
"value1"
],
"name2": [
"value1",
"value2",
"value3",
"value4",
"value5",
],
"name3": [
"value1"
]
}
new to JSON.Please help me to read this name and value in this file
You need to supply the key of each array like name1
, name2
, name3
etc. So it is better to set a limit for the names like 10. Following code will parse the json file as you expected
HashMap<String, ArrayList<String>> map=new HashMap<>();
int TOTAL_NAMES=10;
try {
JSONObject jsonObject=new JSONObject(json);
for(int k=0; k<TOTAL_NAMES-1; k++) {
String name="name"+k;
JSONArray jsonValueArray1 = jsonObject.getJSONArray(name);
ArrayList<String> valueList=new ArrayList<>();
for (int i = 0; i < jsonValueArray1.length(); i++) {
valueList.add(jsonValueArray1.getString(i));
}
map.put(name, valueList);
}
} catch (JSONException e) {
e.printStackTrace();
}