代码如下:
for (int i1 = 0, l = list.size(); i1 < l; i1++) { Object[] t = (Object[]) list.get(i1); node = new Node(t[1].toString(), t[0].toString()); nodeList.add(node); List l1 = new ArrayList(); List l2 = new ArrayList(); Map m = new HashMap(); m.put("name", personPlace); Map m2 = new HashMap(); m2.put("name", t[1].toString()); l1.add(m); l1.add(m2); Map m3 = new HashMap(); m3.put("name", t[1].toString()); m3.put("value", t[0].toString()); l2.add(m); l2.add(m3); placesLink.add(l1); PlaceCounts.add(l2); String a = JsonMapperUtils.objectToJson(l2); String b = JsonMapperUtils.objectToJson(PlaceCounts); String c = JsonMapperUtils.objectToJson(m3); String d = JsonMapperUtils.objectToJson(l1); }
输出结果:
一维数组:
String[] a = new String[phoneList.size()];
二维数组:
String[][] b =new String[phoneList.size()][2];
map:
m: {name=北京} m3:{name=南昌, value=2}
list: l1:[{name=北京}, {name=南昌}] l2:[{name=北京}, {name=南昌, value=2}]
list中的list placeCounts:[[{name=北京}, {name=南昌, value=2}]] 对应的json格式:
a:
["a","b","c"]
b:
[["a","b"],["a1","b1"],["c1","d1"],["c12","d22"]] m3: {"name":"南昌","value":"2"} l2: [{"name":"北京"},{"name":"南昌","value":"2"}] placeCounts: [[{"name":"北京"},{"name":"南昌","value":"2"}]] [[{"name":"北京"},{"name":"南昌","value":"2"}],[{"name":"北京"},{"name":"重庆","value":"4"}]]
Map<String, Object> info = new HashMap<String, Object>();
info.put("nodeList", nodeList); info.put("placesLink", placesLink); //数组 info.put("PlaceCounts", PlaceCounts); //数组 String json = JsonMapperUtils.objectToJson(info);
输出结果
{"phoneNums":["北京"],"nodeList":[{"name":"南昌","value":"2"},{"name":"重庆","value":"4"},{"name":"拉萨","value":"2"},{"name":"上海","value":"1"}],
"PlaceCounts":[["name:北京","name:南昌,value:2"],["name:北京","name:重庆,value:4"],["name:北京","name:拉萨,value:2"],["name:北京","name:上海,value:1"]],
"success":true, "placesLink":[["{name:北京}","{name:南昌}"],["{name:北京}","{name:重庆}"],["{name:北京}","{name:拉萨}"],["{name:北京}","{name:上海}"]]}
Map<String, Object> info = new HashMap<String, Object>(); info.put("nodeList", nodeList); info.put("placesLink", placesLink); //list<list<map<key,value>>> String json = JsonMapperUtils.objectToJson(info);
输出结果
{"phoneNums":["北京"],
"nodeList":[{"name":"南昌","value":"2"},{"name":"重庆","value":"4"},{"name":"拉萨","value":"2"},{"name":"上海","value":"1"}],
"PlaceCounts":[[{"name":"北京"},{"name":"南昌","value":"2"}],[{"name":"北京"},{"name":"重庆","value":"4"}],[{"name":"北京"},{"name":"拉萨","value":"2"}], [{"name":"北京"},{"name":"上海","value":"1"}]],
"success":true,
"placesLink":[[{"name":"北京"},{"name":"南昌"}],[{"name":"北京"},{"name":"重庆"}],[{"name":"北京"},{"name":"拉萨"}],[{"name":"北京"},{"name":"上海"}]]}