0

Helllo, I am trying to send a list of Objects from spring controller and using that list in javascript from jsp in order to make a chart. Since javascript is taking the list as a string, can anyone suggest me a proper method of doing this. Im trying to use this list to iterate in order to put values for a chart...

List list=WebKinmelServiceManager.select("select i From Item i", Item.class);\
        mav.addObject("list", list);

and i want to display in jsp page inside javascript in this format..

$scope.data = {
    series: ['Sales', 'Income', 'Expense'],
    data : [{
        x : "Jack",
        y: [100,210, 384],
        tooltip:"this is tooltip"
    },
    {
        x : "John",
        y: [300, 289, 456]
    },
    {
        x : "Stacy",
        y: [351, 170, 255]
    },
    {
        x : "Luke",
        y: [54, 341, 879]
    }]     
}

Would really appreciate :)

2 Answers 2

2

I recommend this way -> Java list to JSON( here is good library for that )

https://github.com/google/gson

here is the sample code

List<SomeObject> objList = new ArrayList<SomeObject>();
objList.add(new SomeObject());

//make json string
new Gson().toJson( objList );
Sign up to request clarification or add additional context in comments.

2 Comments

Could you please mention how to use that object in javascript
var jsonString = '{"result":true,"count":1}'; var jsonObj =JSON.parse(json); console.log(jsonObj.result); console.log(jsonObj.count); you can access any property and value, like a javascript object.
0

Found some libraries here which can be easily imported

My code went something like

My Controller

JSONArray series=new JSONArray();
            series.put("Quantitiy");
            series.put("Price");
        mav.addObject("series", series);
        JSONArray array=new JSONArray();
        List list=WebKinmelServiceManager.select("select i From Item i", Item.class);
        for (Object object : list) {
            Item item=(Item) object;
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("x", item.getName());
                JSONArray array2=new JSONArray();
                array2.put(item.getQuantity());
                array2.put(item.getPrice());
            jsonObject.put("y", array2);

            array.put(jsonObject);
        }
        mav.addObject("data", array);

And my Javascript went like

var data=${data};
var series=${series}
$scope.data = {
    series: series,
    data : data
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.