-4
{
"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

kamal verma
  • 496
  • 3
  • 16
Poorna
  • 5

1 Answers1

-1

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();
    }
Mars Moon
  • 94
  • 2
  • 14
  • Thanks for your help.but the names are dynamic so i should use some position values to retrieve the name and value as well – Poorna Jan 22 '18 at 08:59