0
我創建了這樣一個REST服務:從JSON數組到JSON對象轉換工作不正常
var rootURL = "http://localhost:8181/RestServiceProject/rest/WebService/getsummary";
function findBySummary() {
console.log("inside loading grid");
var returnval1;
$.ajax({
type: 'GET',
url: rootURL,
dataType: "json",
async: false,
success: function(json3){
returnval1 = json3;
console.log(returnval1);
}
});
return returnval1;
}
console.log(returnval1);
回報:
@GET
@Path("/getsummary")
@Produces("application/json")
public Response summary()
{
VoltDAOImpl voltDao = new VoltDAOImpl();
Map<String ,List<HashMap<String,String>>> returnList= voltDao.getOrderDetails("PEAKM" , "Hydra" ,
"" , voltDao.client,"notional" ,1000);
List<HashMap<String,String>> totalSlpSummaryList = returnList.get("total_slp_summary");
List<HashMap<String,String>> totalSlpSummaryBySideList = returnList.get("total_slp_summary_by_side");
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String json1 = null;
String json2 = null;
String json3 = null;
try {
json1 = ow.writeValueAsString(totalSlpSummaryList);
json2 = ow.writeValueAsString(totalSlpSummaryBySideList);
json3="["+json1+","+json2+"]";
//json3 = json1 + "\n" + json2;
System.out.println(json1);
System.out.println(json2);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Response.status(200).entity(json3).build();
}
然後我在這樣一個javascript這個消費結果如下: returnval1
後來我打電話findBySummary();
並執行以下操作:
var json3 = findBySummary();
var json4 = json3[0];
console.log("json4");
console.log(json4);
var json_temp = JSON.stringify(json4);
console.log("json_temp")
console.log(json_temp);
var json_temp1 = json_temp[0];
console.log("json temp1");
console.log(json_temp1);
下面是此日誌輸出: log output
我想轉換json_temp,這就是:
[{"total_exec_qty":"286595","total_notional":"21820771.72","total_wt_arr_last_slp":"2.4364","total_num_ords":"1630","total_wt_ivwap_slp":"6.0969","total_wt_arr_slp":"1.7889","total_ord_qty":"576991"}]
這個(json_temp1):
{"total_exec_qty":"286595","total_notional":"21820771.72","total_wt_arr_last_slp":"2.4364","total_num_ords":"1630","total_wt_ivwap_slp":"6.0969","total_wt_arr_slp":"1.7889","total_ord_qty":"576991"}
但是爲什麼我只爲json_temp1獲得[
?