Json string of turn two-dimensional array

 

Due to a recent report needs soft sail, own Baidu for a long time but could not find the right, simply write yourself a program not too much testing, what problems can leave a message for me.

 

note:

Incoming Json required by List <Object> transformed from

例如:[{"name":"zhangsan","age":"20"},{"name":"lisi","age":"21"}]

The result will be a similar list -style string two-dimensional array

Example:

name age
zhangsan 20
lysis 21

 

 

 

 

 

Code:

 1     public static String[][] convert(String json) {
 2         String result[][] = null;
 3         String keys[] = null;
 4         List<String> keysList = new ArrayList<String>(10);
 5         JSONArray objects = JSONObject.parseArray(json);
 6 
 7         for (int i = 0; i < objects.size(); i++) {
 8             JSONObject jsonObject = objects.getJSONObject(i);
 9             Map<String, Object> map = jsonObject.getInnerMap();
10             Iterator<String> iter = map.keySet().iterator();
11             if (i == 0) {
12                 while (iter.hasNext()) {
13                     String key = iter.next();
14                     keysList.add(key);
15                 }
16                 keys = keysList.toArray(new String[keysList.size()]);
17                 result = new String[jsonObject.size() + 1][keys.length];
18                 result[0] = keys;
19             }
20             for (int j = 0; j < keys.length; j++) {
21                 result[i + 1][j] = map.get(keys[j]).toString();
22             }
23         }
24         return result;
25     }

 

Guess you like

Origin www.cnblogs.com/zhezhao/p/11223437.html