josn转换为数组
请教高手,java 如何将此格式
[
{"104680BE-A2F9-433C-AF45-22612D6B6038":"CM201200604",
"E5D18609-892F-4A82-AEEE-EFFF5C15996A":"CM201200201"
]
转换正这种格式
[
["104680BE-A2F9-433C-AF45-22612D6B6038","CM201200604"],
["E5D18609-892F-4A82-AEEE-EFFF5C15996A","CM201200201"]
]
------解决方案--------------------
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JsonUtils {
public static String encodeList(Object[] objs) {
return encodeList(Arrays.asList(objs));
}
@SuppressWarnings("unchecked")
public static String encodeList(Collection objs) {
JSONArray list = new JSONArray();
if (objs == null
------解决方案-------------------- objs.size() == 0)
return list.toString();
for (Object ae : objs) {
list.add(ae);
}
return list.toString();
}
@SuppressWarnings("unchecked")
public static String encodeObject(Object obj) {
if (obj instanceof Collection)
return encodeList((Collection) obj);
JSONObject jo = JSONObject.fromObject(obj);
return jo.toString();
}
@SuppressWarnings("unchecked")
public static <T> List<T> decodeList(String str, Class<T> clazz) {
if (str == null
------解决方案-------------------- "".equals(str))
return null;
JSONArray obj = JSONArray.fromObject(str);
Object[] rts = obj.toArray();
List<T> result = new ArrayList<T>(rts.length);
for (int i = 0; i < rts.length; i++) {
Object jo = rts[i];
T ele = (T) JSONObject.toBean((JSONObject) jo, clazz);
result.add(ele);
}
return result;
}
@SuppressWarnings("unchecked")
public static <T> T decodeObject(String json, Class<T> clz) {
JSONObject jsonObject = JSONObject.fromObject(json);
T bean = (T) JSONObject.toBean(jsonObject, clz);
return bean;
}
public static void main(String[] args) {
String s="[{\"104680BE-A2F9-433C-AF45-22612D6B6038\":\"CM201200604\",\"E5D18609-892F-4A82-AEEE-EFFF5C15996A\":\"CM201200201\"}]";
List<Map> l =decodeList(s,Map.class);
List<List> a=new ArrayList<List>();
for(Map m:l){
for(Object st:m.entrySet()){
Map.Entry<String, String> ma=(Map.Entry<String, String>)st;
List<String> n=new ArrayList<String>();
n.add(ma.getKey());
n.add(ma.getValue());
a.add(n);
}
}
System.out.println(encodeList(a));
}
}