如何将json格式的string字符串转换为string数组?.. |
发布时间:2011-05-17 |
www.json.org上有很多Java 楼主2010-10-10 4:58:31 比如一个string类型的json数组,Java code[{"type":"城际高速","station":"北京南","stationNO":"1","days":"1","arriveTime":"-","startDriveTime":"08:45","km":"0"},{"type":"城际高速","station":"天津","stationNO":"2","days":"1","arriveTime":"09:15","startDriveTime":"-","km":"120"}] 1楼 推这个,小巧好用。 JSON-Simple 2楼 你可以去看看nutz这个工具 3楼 String和JSON相互转换速度快,nutz 4楼 谢谢楼上两位。程序上如何实现啊?我试了好长时间没成功...希望哪位大哥能帮帮忙 5楼 实在不行就自己拆 先用“,”分割 再用“:”分割 6楼 用JSON-Simple做的,结果是一个二为阵列,至於输出格式,这个你可以自己修正。 Java codepublicstaticvoidmain(String[] args){ String s="[{\"type\":\"城际高速\",\"station\":\"北京南\",\"stationNO\":\"1\",\"days\":\"1\",\"arriveTime\":\"-\",\"startDriveTime\":\"08:45\",\"km\":\"0\"},{\"type\":\"城际高速\",\"station\":\"天津\",\"stationNO\":\"2\",\"days\":\"1\",\"arriveTime\":\"09:15\",\"startDriveTime\":\"-\",\"km\":\"120\"}]"; Object obj=JSONValue.parse(s); JSONArray array=(JSONArray)obj; Iterator iter=array.iterator(); String[][] ary=newString[array.size()+1][7]; String[] ss={"type","station","stationNO","days","arriveTime","startDriveTime","km"};int[] a={1,2,3}; ary[0]=ss;inti=1;while(iter.hasNext()){ JSONObject jObj=(JSONObject)iter.next(); ary[i]=newString[7]; ary[i][0]=(String)jObj.get("type"); ary[i][1]=(String)jObj.get("station"); ary[i][2]=(String)jObj.get("stationNO"); ary[i][3]=(String)jObj.get("days"); ary[i][4]=(String)jObj.get("arriveTime"); ary[i][5]=(String)jObj.get("startDriveTime"); ary[i][6]=(String)jObj.get("km"); i++; } System.out.println(Arrays.toString(ary[0])); System.out.println(Arrays.toString(ary[1])); System.out.println(Arrays.toString(ary[2])); } |
如何将json格式的string字符串转换为string数组
最新推荐文章于 2024-09-13 09:42:31 发布