By implementing this code, I was able to create the following structure
int n=3;
String json []= new String [n];
try {
JSONArray js = new JSONArray();
ArrayList<String> ciudades;
ciudades = new ArrayList<String>();
ciudades.add("tokio");
ciudades.add("madrid");
ciudades.add("santiago");
JSONObject j;
for (int x = 0; x < ciudades.size(); x++) {
ArrayList<Integer> temp;
temp = new ArrayList<Integer>();
for(int z=0;z<6;z++){
int temperatura = x+z;
temp.add(temperatura);
}
j = new JSONObject();
j.put("name", ciudades.get(x));
j.put("data", temp);
js.put(j);
}
json[0] = js.toString();
} catch (Exception e) {
System.out.println(e.getMessage());
}finally {
String valor="json1";
request.setAttribute(valor, json[0]);
RequestDispatcher dispatcher = context.getRequestDispatcher("/datos.jsp");
dispatcher.forward(request, response);
The generated structure looks like this
[
{"name":"tokio","data":[0,1,2,3,4,5]},
{"name":"madrid","data":[1,2,3,4,5,6]},
{"name":"santiago","data":[2,3,4,5,6,7]}
]
The desired structure that needs to be created is as follows
"paises": {
"pais": [
{"name":"tokio","data":[0,1,2,3,4,5]},
{"name":"madrid","data":[1,2,3,4,5,6]},
{"name":"santiago","data":[2,3,4,5,6,7]}
]
}
When receiving the variables in JavaScript
var np= ${json1};
var datos = np;
To achieve the desired list structure and read the second level structure in JavaScript, further steps are needed.