我正在使用Google图表API和 Google Visualization Candlestick Charts期望数据是一个数组数组,以便它可以工作。
例如以下格式
[
[ "2011-08-01", 136.65, 136.96, 134.15, 136.49 ],
[ "2011-08-02", 135.26, 135.95, 131.50, 131.85 ],
[ "2011-08-05", 132.90, 135.27, 128.30, 135.25 ]
]
这是我通过数据库检索数据的SQL
String selectsql = "select current_day , open_val , high_val , low_val , close_val from historical_data where symbol_name = ?";
while(Rset.next())
{
String current_day = SgxRset.getString("current_day");
String open_val = SgxRset.getString("open_val");
String high_val = SgxRset.getString("high_val");
String low_val = SgxRset.getString("low_val");
String close_val = SgxRset.getString("close_val");
}
您能否告诉我如何构建如何构造数组?
示例程序
import java.util.ArrayList;
import java.util.List;
import org.json.JSONException;
public class TestJSON {
public static void main(String[] args) throws JSONException {
Employee emp1 = new Employee();
emp1.setName("Ravi");
emp1.setName("20");
Employee emp2 = new Employee();
emp2.setName("Kiran");
emp2.setName("20");
List<Employee> histList = new ArrayList<Employee>();
Object[] arrayResultData = histList.toArray();
}
}
public class Employee {
String name ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
String age ;
}
答案 0 :(得分:1)
I recommend a List. You don't know what your columns are and how many you have:
List<HistData> histList = new ArrayList<HistData>();
//.. query db
while(Rset.next()){
HistData histData = new HistData();
histData.setCurrentDay( SgxRset.getString("current_day"));
histData.setOpenVal( SgxRset.getString("open_val"));
histData.setHighVal( SgxRset.getString("high_val"));
histData.setLowVal( SgxRset.getString("low_val"));
histData.setCloseVal( SgxRset.getString("close_val"));
histList.add(histData);
}
HistData is just a POJO,但您可以更改bean和SgxRset检索类型中的数据类型,这样您就不会被锁定到任何数组类型。使用List而不是数组可以在返回许多未知长度的结果时提供更大的灵活性。
public class HistData {
public HistData(){}
String currentDay;
public setCurrentDay(String currentDay){
this.currentDay = currentDay;
}
public getCurrentDay(){
return currentDay;
}
// .. etc.
}
答案 1 :(得分:1)
使用getter和setter创建一个将所有这些数据保存在这样的对象中的类:
public class Data {
private String current_day;
private double open_val;
private double high_val;
private double low_val;
private double close_val;
public Data(String current_day, double open_val, double high_val,
double low_val, double close_val) {
this.current_day = current_day;
this.open_val = open_val;
this.high_val = high_val;
this.low_val = low_val;
this.close_val = close_val;
}
public String getCurrent_day() {
return current_day;
}
public void setCurrent_day(String current_day) {
this.current_day = current_day;
}
public double getOpen_val() {
return open_val;
}
public void setOpen_val(double open_val) {
this.open_val = open_val;
}
public double getHigh_val() {
return high_val;
}
public void setHigh_val(double high_val) {
this.high_val = high_val;
}
public double getLow_day() {
return low_val;
}
public void setLow_day(double low_day) {
this.low_val = low_day;
}
public double getClose_day() {
return close_val;
}
public void setClose_day(double close_day) {
this.close_val = close_day;
}
}
现在,将数据存储在您从SQL获取的对象数组中。
String selectsql = "select current_day , open_val , high_val , low_val ,close_val from historical_data where symbol_name = ?";
List<Data> myList = new ArrayList(Data);
while(Rset.next())
{
String current_day = SgxRset.getString("current_day");
String open_val = SgxRset.getString("open_val");
String high_val = SgxRset.getString("high_val");
String low_val = SgxRset.getString("low_val");
String close_val = SgxRset.getString("close_val");
Data data = new Data(current_day,open_val,high_val,low_val,close_val);
myList.add(data);
}
对于阅读数据,您可以写:
String a = "[ ";
for (int i = 0; i < myList.size(); i++) {
String b = "[ \"" + myList.get(i).getCurrent_day() + "\" , "
+ myList.get(i).getOpen_val() + "],";
if(i==myList.size() -1)
a += b;
else
a += b+",";
}
a += " ]";
现在,对我来说输出是:
[ [ "2011-08-01" , 136.65],[ "2011-08-02" , 135.26] ]
答案 2 :(得分:1)
使用List和toArray方法
String selectsql = "select current_day , open_val , high_val , low_val , close_val from historical_data where symbol_name = ?";
List<String[]>resultData = new ArrayList<Stirng[]>();
while(Rset.next())
{
String[] array = new String[5];
array[0] = SgxRset.getString("current_day");
array[1] = SgxRset.getString("open_val");
array[2] = SgxRset.getString("high_val");
array[3] = SgxRset.getString("low_val");
array[4] = SgxRset.getString("close_val");
}
Object[] arrayResultData = resultData.toArray();