I don't know much about JavaScript, but I was able to convert an array of double (in Java) to an array in JavaScript too, using json (with Gson).
But now I need to convert this list below in some list of objects, so it can be dynamic on my server side to send to the client side.
plotBands: [{ // Light air
from: 0.3,
to: 1.5,
color: 'rgba(68, 170, 213, 0.1)',
label: {
text: 'Light air',
style: {
color: '#606060'
}
}
}, { // Light breeze
from: 1.5,
to: 3.3,
color: 'rgba(0, 0, 0, 0)',
label: {
text: 'Light breeze',
style: {
color: '#606060'
}
}
}, { // Gentle breeze
from: 3.3,
to: 5.5,
color: 'rgba(68, 170, 213, 0.1)',
label: {
text: 'Gentle breeze',
style: {
color: '#606060'
}
}
}, { // Moderate breeze
from: 5.5,
to: 8,
color: 'rgba(0, 0, 0, 0)',
label: {
text: 'Moderate breeze',
style: {
color: '#606060'
}
}
}, { // Fresh breeze
from: 8,
to: 11,
color: 'rgba(68, 170, 213, 0.1)',
label: {
text: 'Fresh breeze',
style: {
color: '#606060'
}
}
}, { // Strong breeze
from: 11,
to: 14,
color: 'rgba(0, 0, 0, 0)',
label: {
text: 'Strong breeze',
style: {
color: '#606060'
}
}
}, { // High wind
from: 14,
to: 15,
color: 'rgba(68, 170, 213, 0.1)',
label: {
text: 'High wind',
style: {
color: '#606060'
}
}
}]
How should be this class in Java ? So I can use Json (with Gson) to parse it.
I think should be something like:
class Info
private float from;
private float to;
private String color;
private Label label;
class Label
private String text;
private Style style;
class Style
private String color;
And in the end I have a List<Info> plotBands
and use .toJson
method.
Am I thinking right ? Or there's something I'm missing here ?
UPDATE In my .xhtml page I have :
<h:outputScript>var data = ${reportc.dataAsJson};</h:outputScript>
I create these class above that I mentioned: Then I create :
List<Info> infos = new ArrayList<Info>();
infos.add(new Info(0, 10, "rgba(68, 170, 213, 0.1)", new Label("Cold", new Style("#606060"))));
data.put("plotBands", infos);
This generate the follow javascript code:
{"plotBands":[{
"from":0.0,
"to":10.0,
"color":"rgba(68, 170, 213, 0.1)",
"label":{
"text":"Cold",
"style":{
"color":"#606060"
}
}
}]
Which it seems correct, but how to pass this to my javascript code ? I'm trying but nothing seems to work.