将动态json传递给ddslick js插件

时间:2016-03-07 21:36:58

标签: javascript jsp

有人知道将动态生成的json传递给js插件的方法

我正在使用DDslick js插件制作动态下拉列表

http://designwithpc.com/Plugins/ddSlick#demo

DDslick中的项目以json格式硬编码,如此

    var ddData = [
     {
    text: "Facebook",
    value: 1,
    selected: false,
    description: "Description with Facebook",
    imageSrc: "xxxxx"
    },
    {
    text: "Twitter",
    value: 2,
    selected: false,
    description: "Description with Twitter",
    imageSrc: "xxxxx"
     }      
    ];

这个下拉列表必须是动态的,所以现在我用这些值制作一个虚拟的json

            JSONObject json = new JSONObject();
            JSONObject json2 = new JSONObject();
            try {
                json.put("text", "xxxxx");
                json.put("value", "xxxx");
                json.put("description", "xxxx-xxxx-xxxx");
                json.put("imageSrc", "xxxxx");
                json2.put("text", "aaaa");
                json2.put("value", "aaaa");
                json2.put("description", "aaaa-aaaa-aaaa");
                json2.put("imageSrc", "aaaa");
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             string output = json.toString()+","+json2.toString();

并在jsp上将输出提供给插件,但这些都不起作用,下拉列表不可见

             var ddData = eval("${output}");
             var ddData = "${output}";
             var ddData = ["${output}"];

如果我在jsp中对相同的字符串输出进行硬编码,则可以正常工作

var ddData = [
    {"text":"xxx","imageSrc":"xxxx","description":"xxxx-xxxx-xxx","value":"xx"},
    {"text":"aaaa","imageSrc":"aaaa","description":"aaaa-aaaa-aaaa","value":"aaaa"}
];

1 个答案:

答案 0 :(得分:0)

尝试:

    var ddData = JSON.parse( output );

请参阅:http://www.w3schools.com/js/js_json.asp