如何将json格式的string字符串转换为string数组
1个回答
展开全部
public static void main(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 = new String[array.size()+1][7];
String[] ss = {"type", "station","stationNO", "days", "arriveTime", "startDriveTime", "km"};
int[] a = {1,2,3};
ary[0] = ss;
int i=1;
while(iter.hasNext()){
JSONObject jObj = (JSONObject)iter.next();
ary[i] = new String[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]));
}
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 = new String[array.size()+1][7];
String[] ss = {"type", "station","stationNO", "days", "arriveTime", "startDriveTime", "km"};
int[] a = {1,2,3};
ary[0] = ss;
int i=1;
while(iter.hasNext()){
JSONObject jObj = (JSONObject)iter.next();
ary[i] = new String[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]));
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询