XStream类对象把List<javaBean>()转成json数据

baijinswpu 2019-12-28

【省市联动】

Servlet端:

XStream把list转成json数据
//JSONArray-->变成数组/集合[]
//JSONObject-->变成简单的数据{name:ayee, pid:1}
JSONArray jsonArray = JSONArray.fromObject(list);
String json = jsonArray.toString();

js端:

$(function(){
    $("#province").change(function(){
        var pids = $(this).val();
        
        /*[
            {
                "cname":"福州市",
                "id":1,
                "pid":1
            },
            {
              "cname":"厦门市",
              "id":2,
              "pid":1
            },
            ...
        ]*/
        
        $.post("CityServlet",{pid:pids}, function(data,status){
            $("#city").html("<option value=‘‘>-请选择-</option>");
            $(data).each(function(index, c){
//                alert(c.cname);
                $("#city").append("<option value=‘" + c.id + "‘>" + c.cname + "</option>")
            });
        },"json");
    });
});

Json相关jar包:

commons-beanutils.jar

commons-collections.jar

commons-lang.jar

commons-logging.jar

ezmorph.jar

json-lib-2.4-jdk15.jar

相关推荐