-1

I've a method that returns a 2d-array in java : public Object[][] getArray1() {}

I used println() to print it's content and i see that its well created.

On Javascript i assign this array to a var:

var content ="#{BDEStats.getArray1()}";

But i dont seem able to acess it's data. it always returns java.Lang.object; @... How can i do to display the content this array holds?

I've tried to cycle the array but i dont know how to refer to the objects it is holding. if i use content[1] to returns a char in that índex..! Kinda lost here

Tarik
  • 4,961
  • 3
  • 36
  • 67
  • You cant do that this way. I see you are using JSF, what are you trying to do? – Stefan Dec 01 '14 at 15:06
  • I've a graphic that receives a 2d-array https://developers.google.com/chart/interactive/docs/reference#google.visualization.arraytodatatable I built an array that holds the values I want to pass to the graphic.. i printout all the values in java but when i pass the object through JSF i can't acess this array – Salvador Macedo Dec 01 '14 at 15:12
  • Take a look at the answer here: http://stackoverflow.com/questions/14217263/rendering-2d-array-without-hdatatable – GavinBrelstaff Dec 01 '14 at 15:12
  • Ok ive created a JSONObject in a JAVA class with a method that returns it. Can i pass it now through JSF to acess it on the client side? – Salvador Macedo Dec 01 '14 at 17:41
  • https://developers.google.com/chart/interactive/docs/dev/dsl_get_started – Stefan Dec 02 '14 at 08:55

4 Answers4

0

This is the normal array String representation in Java, consisting in:

  • A number of [ based on the dimension
  • Either a letter for the primitive type (i.e. I for int), or L[fully qualified class name] for Objects
  • @
  • The array's hash code

For one-dimensional arrays, use java.util.Arrays.toString(myArray).

For multi-dimensional arrays, use java.util.Arrays.deepToString(myArray).

Edit (adding previous comment to answer)

You probably want to investigate JSON.parse to parse your Java array from JavaScript.

Mena
  • 47,782
  • 11
  • 87
  • 106
0

I think you may convert the array to JSON format before assigning it to javascript.

You can use some JSON framework to do this convert like:

Here a tiny Jackson demo:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

...

public static void main(String[] args) throws JsonProcessingException {
    String[][] data = new String[1][2];
    data[0][0] = "abc";
    data[0][1] = "def";
    System.out.println(new ObjectMapper().writeValueAsString(data));
}
Leon
  • 228
  • 2
  • 8
  • Thank you for your reply. Im investigating Json. So I will not need to pass the array from JAVA directly to read it through JSF if I understood correctly. My 2d array was supposed to be the input of a graphic. this object im returning in Javascript is useless and i cannot parse him directly in JS, correct? – Salvador Macedo Dec 01 '14 at 15:30
0

To turn a Java array into a string representation in a syntax which can be interpreted by a JavaScript engine, you need to turn it into the JavaScript Object Notation, or JSON for short.

There are many libraries available for Java to do this. Software recommendations are off-topic on Stackoverflow, but this article which compares 5 different libraries helped me to pick one for my project.

On the JavaScript side, you just have to use var content = JSON.parse(stringFromJava).

Or when you generate the JS code procedurally on the Java side, you can just embed the JSON string right into the sourcecode. This works because JSON is valid Javascript code for an object literal. In Java, this would look something like this:

 scriptCode.append("var content = " + arrayAsJsonString + ";\n");
Philipp
  • 67,764
  • 9
  • 118
  • 153
0

Ok problem solved. This was how I did it:

Instead of returning a Java Array I returned a JSON object in my method.

This JSON Object has a name and several other fields per ex: (I'm getting my data from a Java List, so I iterate the list to populate the JSON object)

SONObject jsonObj = new JSONObject();
jsonObj.clear();
for (int tt=0;  tamanho>tt ; tt++) {
            try {                
jsonObj.put("aa"+tt, ListaJobStats.get(tt).getName());
jsonObj.put("bb"+tt , new BigDecimal(ListaJobStats.get(tt).getAge() ....

After this if I printOut the JSON object in java i get a string:

aa0: '1st name'; aa1: ' 2nd name' ; bb0: 'age'; bb1: '2nd age' ... etc etc

After this in Javascript i get my JSON Object thorugh JSF like this:

var content=#{JAVACLASS.METHODTHATRETURNSJSON};

I stringify this object in JS:

var pars= JSON.stringify(content);

and i create the JSON object

var json = JSON.parse(pars)

Now I Iterate this JSON object in JS like this:

for (var tt=0;  tamanho>tt ; tt++) {

         [now im specifically adding the values to create a graphic but its just na exemple how u can acess te object]




  data.setValue(tt, 0, json["aa"+tt]);


 data.setValue(tt, 1, json["bb"+tt]); 

...

Hope it will be useful. Take care

  • Stringify + parse steps are completely unnecessary. You're basically massaging it from JSON to String and then again to JSON. Just do `var json = #{JAVACLASS.METHODTHATRETURNSJSON};`. – BalusC Feb 16 '15 at 08:43