鍍金池/ 問答/Java  HTML/ JS解析多層集合json

JS解析多層集合json

public static void main(String[] args) {
    List all = new ArrayList();
    List userlist = new ArrayList();
    userlist.add(new UserModel().set("name", "tom"));
    List userlist1 = new ArrayList();
    userlist1.add(new UserModel().set("name", "tom"));
    all.add(userlist);
    all.add(userlist1);
    renderJson(all);
}

前臺js 怎么解析出all 集合中所有子集中的對象?

回答
編輯回答
舊顏

renderJson(all) 應(yīng)該會得到 [[{"name":"tom"}],[{"name":"tom"}]], 然后你在前臺JavaScript 中let all = JSON.parse('[[{"name":"tom"}],[{"name":"tom"}]]') 就可以得到相應(yīng)的結(jié)果。

2017年9月12日 19:08
編輯回答
深記你

不管幾層集合,后臺保證傳過來的是jsonStr(使用net.sf.json.JSONArray 方便),前臺使用JSON.parse()函數(shù)轉(zhuǎn)為js對象然后在取值就行了(不知道怎么取F12慢慢試一試)

2017年10月8日 15:04
編輯回答
安淺陌

我猜你的 all 長這樣:

var all = [
    [{name:'tom'}],
    [{name:'tom'}]
]

解析出all 集合中所有子集中的對象:

function getObj(arr){
    var tmp = []
    all.forEach(function(item,index){
        item.forEach(function(obj,i){
            tmp.push(obj)
        })
    })
    return obj
}

var all = [
    [{name:'tom'}],
    [{name:'tom'}]
]

getObj(all)
//[{name:'tom'},{name:'tom'}]
2018年2月26日 17:51
編輯回答
撥弦

返回的JSON長啥樣?

2018年8月12日 07:12
編輯回答
臭榴蓮
public void searchDraftProjets(){
    String [] tables = getPara("tabname").split(",");
    String keyword = getPara("keyword");
    String createUser = getSessionAttr("username");
    List<List<Record>> alllist = new ArrayList<List<Record>>();
    int totalCount=0;
    for (int i = 0; i < tables.length; i++) {
        List<Record>list = ProjectModel.projectDao.getProDraftlist(tables[i], keyword,createUser);
        totalCount+=list.size();
        alllist.add(list);
    }
    String json="{\"totalCount\":\""+totalCount+"\",\"draftlist\":\""+alllist+"\"}";

    renderJson(json);
}

到前臺json字符串key value 沒有雙引號了,JSON.parse無法解析
圖片描述

2018年3月25日 17:53