Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
What exactly is the problem? What happens when you run Gson? Does it give the expected JSON string back? –  BalusC Mar 12 '12 at 18:37
    
I want to create a dynamic 'plotBands' in Java so I can use Json to parse it, and I don't know how to do it. –  Valter Henrique Mar 12 '12 at 18:40
    
I understand that you are having problems with it, but I do not see/understand the concrete problem. At what step exactly are you failing? –  BalusC Mar 12 '12 at 18:44
    
@BalusC I update my post, please take a look. –  Valter Henrique Mar 12 '12 at 19:06
add comment

2 Answers

up vote 0 down vote accepted

In your JS file, replace

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'
                    }
                }
            }]

by

plotBands: data.plotBands
share|improve this answer
    
Thank you, I was having problems with 'plotBands' in the server side and with 'plotbands' in the client side. But now everything works fine. Thank you Bauke. –  Valter Henrique Mar 12 '12 at 19:18
add comment
class Style
   private String color;
share|improve this answer
    
thank you Peter, I update my post. –  Valter Henrique Mar 12 '12 at 18:27
    
Am I doing this right ? –  Valter Henrique Mar 12 '12 at 18:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.