0

How can I get the below output in jsp?

[[1417165200000,28477.92],[1417165320000,28484],[1417165440000,28474.86],[1417165560000,28478.88]]

Both the values comes from a variables in jsp code.

I have tried:

Map<String, String> list = new HashMap<String, String>();
list.put(last_traded_price, price_date);

It gives me this:

{28779.4400=2014-11-28 12:58:00.0, 28794.5000=2014-11-28 01:24:00.0}

Please suggest me what I can use in jsp to get the desired output.

This is what I did in php,

$array_item[] = array(strtotime($price_date)*1000, (float)$last_traded_price);

Update 1:

while(rs.next()){

last_traded_price = rs.getString(1);
price_date = rs.getString(2);

child.add(last_traded_price);//1
child.add(price_date);//2

}

1 Answer 1

0

If you want this [[1417165200000,28477.92],[1417165320000,28484]] particular format then i would advice you to use json-simple.jar so if whenever you want to create data in above format you need to write code as below :

JSONArray root = new JSONArray();


while(rs.next()){
  JSONArray child= new JSONArray();
  last_traded_price = rs.getString(1);
  price_date = rs.getString(2);

  child.add(last_traded_price);//1
  child.add(price_date);//2
  root.add(child);
}
System.out.println(root);

so for 1417165200000,28477.92 this pair you will have root as [[1417165200000,28477.92]]

if you want to add another pair just repeat the steps 1,2 and 3

11
  • I am getting this output [["28477.9200","2014-11-28 09:00:00.0","28484.0000","2014-11-28 09:02:00.0"]] updated my question with your suggestions.
    – user3331391
    Nov 28, 2014 at 9:36
  • convert values into their respective data types for eg child.add(Double.parseDouble(oparr[i])); when oparr is my string array. you are storing strings that's why they are enclosed inside double quotes
    – Atul Nar
    Nov 28, 2014 at 9:44
  • No its not regarding the double quotes the brackets should close after each pair.
    – user3331391
    Nov 28, 2014 at 9:51
  • JSONArray root = new JSONArray(); while(rs.next()) { last_traded_price = rs.getString(1); price_date = rs.getString(2); child.add(last_traded_price));//1 child.add(price_date);//2 root.add(child); } System.out.println("Root = "+root);
    – Atul Nar
    Nov 28, 2014 at 9:53
  • you forgot to create root array.add these child arrays in root array as shown above . Required data format is stored in root array.
    – Atul Nar
    Nov 28, 2014 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.