After receiving a JSON object from a web server in response, I am encountering some challenges with extracting and displaying the data. The JSON array consists of monthly key-value pairs like {"JAN":"17"}, {"FEB":"19"}, {"MAR":"21"}, etc.
To display these keys dynamically on the x-axis of a chart, I have attempted to extract the keys into an array using JavaScript. However, my current approach doesn't seem to be working as expected.
var al_key = [];
//For loop for x axis dispaly.
alert("al_key.length --------->"+al_key.length);
var jsonObj = []; //declare array
for (var i = 0; i < al_key.length; i++) {
jsonObj.push({value: i, text: al_key[i].value});
}
// Add axes
chart.addAxis("x", {min:1, max: 12 ,labels: jsonObj , fixLower: "major", microTicks: true, majorTickStep: 1});
chart.addAxis("y", { min: 0, max: 50, vertical: true, fixLower: "major", fixUpper: "major", includeZero: true });
I'm seeking advice on where I might be going wrong in this process or if there are alternative methods to achieve the desired result of dynamically displaying months on the x-axis. Any suggestions or help would be greatly appreciated.
Additional Response:
Recently, when dealing with a JSONArray object containing single JSONObjects, I ran into some issues while trying to access and manipulate the data present within it.
To provide an example scenario, consider the following code snippet showcasing server-side coding:
JSONObject object=new JSONObject();
object.put("JAN":"17");
object.put("FEB":"19");
object.put("MAR":"21");
object.put("APR":"23");
object.put("MAY":"24");
object.put("JUN":"27");
JSONArray arrayObj=new JSONArray();
arrayObj.add(object);
System.out.println(arrayObj); // Output: {"JAN":"17"},{"FEB":"19"},{"MAR":"21"},{"APR":"23"},{"MAY":"24"},{"JUN":"27"}];
In a JSP context, further actions need to be taken such as fetching desired values from the database and handling responses accordingly:
//Call to the database to fetch the desired value
dojo.xhrGet( { url : "/POC/Action.do",
handleAs : "json",
sync: true,
load : function(response, ioArgs) {
alert("retrived response ------"+response);
for(var i in response)
for(var x in response[i])
output.push(response[i][x]);
alert("Get value to draw line chart---------------------->"+output);
},
error: function(response, ioArgs){
dojo.byId("grid").innerHTML = "An error occurred, with response: " + response;
return response;
},
handleAs: "json" });
The objective here is to extract keys from the response object and prepare them in a format suitable for displaying on the x-axis of a chart. If any insights or recommendations can be provided regarding this process, please feel free to share your thoughts.