控制器方法:
@RequestMapping(value = "channelIntentionDetails.html", method = RequestMethod.POST)
public @ResponseBody
Report getChannelIntentionDetails(@RequestBody SearchParameters searchParameters) {
LOGGER.info("In ReportController.getChannelIntentionDetails(...), searchParameters " + searchParameters);
return channelDetailsService.getIntentionDetails(searchParameters);
}
报告POJO:
public class Report {
private Map<String, Collection<String>> parameterWiseResult;
private Collection<String> results;
private String result;
private String spid;
.
.
.
}
结果集合包含从MongoDB集合返回的JSON字符串
JS AJAX片段:
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST","channelIntentionDetails.html",false);
xmlhttp.setRequestHeader("Content-Type","application/json");
xmlhttp.setRequestHeader("Accept","application/json");
xmlhttp.send(stringifiedSearchParameter);
alert("Plain AJAX response "+ xmlhttp.response);
alert("After JSON.parse(...) "+ (JSON.parse(xmlhttp.response)).results);
drawIntentionPieChart((JSON.parse(xmlhttp.response)).results);
function drawIntentionPieChart(data) {
//alert("In drawIntentionPieChart(...) " + intentionGooglePieChart);
if (intentionGooglePieChart == null
|| intentionGooglePieChart == 'undefined') {
//alert("Creating new intentionPiechart");
intentionGooglePieChart = new google.visualization.PieChart(
document.getElementById('intentionPiechart'));
}
intentionGooglePieChart.clearChart();
//var jsonData = JSON.parse(data);
var jsonData = data;
var data = new google.visualization.DataTable();
data.addColumn('string', 'Intention');
data.addColumn('number', 'Share');
for (i = 0; i < jsonData.length; i++) {
/* alert("Intention : " + jsonData[i]._id.Intention_category
+ " Count : " + jsonData[i].count); */
data.addRows([ [ jsonData[i]._id.Intention_category,
parseInt(jsonData[i].count) ] ]);
}
var options = {
title : 'Intention Analysis',
titleTextStyle : {
color : '#0E5EAE'
},
fontSize : 14,
width : 390,
height : 200
};
intentionGooglePieChart.draw(data, options);
}
第一次提醒,其中“results”是一个数组
Plain AJAX response {"parameterWiseResult":null,"results":["{ \"_id\" : { \"SpId\" : 352 , \"Intention_category\" : \"Opine\" , \"Report_Id\" : 2 , \"Channel_Id\" : 1} , \"count\" : 1}","{ \"_id\" : { \"SpId\" : 352 , \"Intention_category\" : \"Wish,Purchase\" , \"Report_Id\" : 2 , \"Channel_Id\" : 1} , \"count\" : 1}","{ \"_id\" : { \"SpId\" : 352 , \"Intention_category\" : \"Complain\" , \"Report_Id\" : 2 , \"Channel_Id\" : 1} , \"count\" : 1}","{ \"_id\" : { \"SpId\" : 352 , \"Intention_category\" : \"Purchase\" , \"Report_Id\" : 2 , \"Channel_Id\" : 1} , \"count\" : 2}","{ \"_id\" : { \"SpId\" : 352 , \"Intention_category\" : \"None\" , \"Report_Id\" : 2 , \"Channel_Id\" : 1} , \"count\" : 93}"],"result":null,"spid":null,"idvallistsearchprofile":null,"idvallisttags":null,"spmaster":null,"competitiveParameters":null}
第二个警告在JSON.parse(...)之后,数组括起来。 []不见了:
After JSON.parse(...) { "_id" : { "SpId" : 352 , "Intention_category" : "Opine" , "Report_Id" : 2 , "Channel_Id" : 1} , "count" : 1},{ "_id" : { "SpId" : 352 , "Intention_category" : "Wish,Purchase" , "Report_Id" : 2 , "Channel_Id" : 1} , "count" : 1},{ "_id" : { "SpId" : 352 , "Intention_category" : "Complain" , "Report_Id" : 2 , "Channel_Id" : 1} , "count" : 1},{ "_id" : { "SpId" : 352 , "Intention_category" : "Purchase" , "Report_Id" : 2 , "Channel_Id" : 1} , "count" : 2},{ "_id" : { "SpId" : 352 , "Intention_category" : "None" , "Report_Id" : 2 , "Channel_Id" : 1} , "count" : 93}
稍后,当我尝试迭代此解析结果时,我收到错误。
TypeError:jsonData [i] ._ id未定义
data.addRows([ [ jsonData[i]._id.Intention_category,
parseInt(jsonData[i].count) ] ]);
我在哪里弄乱?
答案 0 :(得分:0)
有两种解决方案可以解决您的问题。
选项1:更改从服务返回的数据格式
如果您想从Web服务返回对象的集合/数组,您的POGO应如下所示。
public class Report {
private Map<String, Collection<String>> parameterWiseResult;
private Collection<MyObject> results;
private String result;
private String spid;
//Setters and Getters
}
public class MyObject {
private Id _id;
private int count;
//Setters and Getters
private class Id {
int SpId;
String Intention_category;
int Report_Id;
int Channel_Id;
//Setters and Getters
}
}
选项2:双解析字符串以在Javascript中将内部String转换为Object。在for循环中,您需要将数组的每个元素解析为JSON,如下所示:
var myJson = JSON.parse(jsonData[i]);
data.addRows([ [ myJson._id.Intention_category, parseInt(myJson.count) ] ]);