2012-03-27 62 views
0

任何人都可以请帮我理解我应该如何访问js中的json数据。 JSP-struts代码访问。我正在使用struts2,json,dojo。但是让问题访问数据。如何在dojo中使用json数据

var chartData = dojo.xhrGet({ 
     url : "getJSONResult", 
     handleAs : "json", 
     preventCache : false, 
     load : function(data) { 
      //how to process 

     } 
    }); 

我在struts.xml

<package name="json" namespace="/" extends="json-default"> 
    <action name="getJSONResult" method="execute" 

class="uk.co.bandc.businessmonitor.web.controller.ShowTransactionAction"> 
     <result type="json" /> 
    </action> 
</package> 

我的动作类

package uk.co.bandc.businessmonitor.web.controller; 

import com.opensymphony.xwork2.ActionSupport; 

public class ShowTransactionAction extends ActionSupport { 
/** 
* 
*/ 
private static final long serialVersionUID = 1L; 

int[] numberarray1 = { 10000, 9200, 11811, 12000, 7662, 13887, 14200, 12222, 12000,  
10009, 11288, 12099 }; 

public String execute() { 

    return SUCCESS; 
} // End execute() 

public int[] getNumberarray1() { 
    return numberarray1; 
} 

public void setNumberarray1(int[] numberarray1) { 
    this.numberarray1 = numberarray1; 
} 

} // End class 
+0

请修复代码格式,谢谢。 – bernie 2012-03-27 16:39:01

+0

您遇到的问题是什么?你的AJAX回调函数中的数据不包含JSON吗?如果是这样,首先要测试的是,当您使用键入浏览器位置的URL访问您的操作时,是否将JSON数据返回到浏览器中? – 2012-03-27 16:59:47

+0

是的,如果我访问http://127.0.0.1:8080/bizmon-web-user/getJSONResult我得到的结果{“numberarray1”:[10000,9200,11811,12000,7662,13887,14200,12222,12000, 10009,11288,12099]}。我想使用数组中的值作为图表的输入。但不知道该怎么做。 – JavaOyeOye 2012-03-27 17:07:13

回答

0

你想画什么样的图表?你的数据似乎是一维的。典型地,对于笛卡尔图表(条形图,线等),就需要2点维的数据,例如:

var exampleData = 
[ 
{ time: 10, count: 7382 }, 
{ time: 20, count: 1852 }, 
{ time: 35, count: 2397 }, 
{ time: 50, count: 1442 }, 
{ time: 55, count: 1854 } 
]; 

然后可以使用该dojox.charting.Chart2D来呈现图表

http://dojotoolkit.org/documentation/tutorials/1.6/charting/见例子为

+0

如果你将在页面上看到http://dojotoolkit.org/documentation/tutorials/1.6/charting/月销售饼图你可以看到var chartData = [10000,9200,11811,12000,7662,13887,14200 ,12222,12000,10009,11288,12099];正在使用。我的问题是如何使用这些数据,如果我从服务器获取格式为{“numberarray1”:[10000,9200,11811,12000,7662,13887,14200,12222,12000,10009,11288,12099]}你试过 – JavaOyeOye 2012-03-28 09:09:27

+0

:chart.addSeries(“SalesThisDecade”,data.numberarray1); – 2012-03-28 13:55:00

+0

耶现在解决了。谢谢。其实我在语法上做了错误,现在已经解决了。 – JavaOyeOye 2012-03-28 14:35:07