2 Replies Latest reply on Jan 12, 2011 12:59 PM by feuyeux

    how to put double array into json

    feuyeux

      scenario:

      retrive db to obtain and calculate data, then return a double array to front tier.

      In front tier, jQuery Chart Tool-- Highcharts will use the result as yaxis input.

      -- -- -- --

      issue:

      in the result, every element includes a quote sign.

      -- -- -- --

      implementation:

      do not use 3rd party jar excluding richfaces.

       

      import org.richfaces.json.JSONArray;

      import org.richfaces.json.JSONException;

      import org.richfaces.json.JSONObject;

       

          for (int i = 0; i < rowKeys.length(); i++) {

                      double[] datas = chartModel.getData()[i];

                      String[] s=new String[datas.length];

                      for (int j = 0; j < s.length; j++) {

                          s[j]=""+datas[j];

                      }               

                       new JSONArray(Arrays.asList(s))); // since JSONArray only receive string array.

                  }

      -- -- -- --

      resolve:

      JSONArray jsonArray =  new JSONArray();

      for (int j = 0; j < datas.length; j++) {

            jsonArray.put(datas[j]);

      }

      -- -- -- --

      question:

      I fish for this convert action, and find JSON-lib has an approach:

      int[] numbers = { 1, 2, 3 };

      jsArray = JSONArray.fromObject(numbers);

       

      I think this is a convenient way to convert.

      So, does json in richfaces have this kind of method I do not know or not.

      If have, please give me an example. thanks a lot!

      I do not wanna include 3rd party jars.

       

      Lu H.