淘先锋技术网

首页 1 2 3 4 5 6 7

注意事项

1:对应元素值必须有引号,否则解析会失败

2:外层必须由[ ]包着

需引入的包

  1. commons-lang.jar
  2. commons-beanutils.jar
  3. commons-collections.jar
  4. commons-logging.jar 
  5. ezmorph.jar
  6. json-lib-2.2.2-jdk15.jar

public class strToJson  {




public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "[{element=990, referralcode=shunnian, screenname=谢顺年, wechatPhoto=upload/photo/20160725/1469435345843.jpg, a9=100, id=16070917195431397101, a1=0, a2=10, a3=30, a4=0, a5=0, a6=50, personalitySimilarity=80, a7=20, a8=0},{element=990, referralcode=shunnian, screenname=谢顺年, wechatPhoto=upload/photo/20160725/1469435345843.jpg, a9=100, id=16070917195431397101, a1=0, a2=10, a3=30, a4=0, a5=0, a6=50, personalitySimilarity=80, a7=20, a8=0}]" ;  // 一个未转化的字符串
str=str.replace("=", "='");
str=str.replace(",", "',");
str=str.replace("}'", "'}");
str=str.replace("}]", "'}]");
System.out.println(str);


JSONArray json = JSONArray.fromObject(str ); // 首先把字符串转成 JSONArray  对象

System.out.println(json);


//如果要转List格式,可以这么做,举个List<Map<String,Object>>例子

List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

list = JSONArray.toList(json ,Map.class);//这里的t是Class<T>

}




}