com.alibaba.fastjson.JSONArray cannot be cast to java.lang.String
List<BillTrackingInfo> billTrackingInfo=trackDepartureCarInfoBiz.getInfoByTrid(trackNumber);
return JSON.toJSON(billTrackingInfo) ;
}
报错:
Servlet.service() for servlet [default] in context with path [/szgz] threw exception [java.lang.ClassCastException: com.alibaba.fastjson.JSONArray cannot be cast to java.lang.String] with root cause
java.lang.ClassCastException: com.alibaba.fastjson.JSONArray cannot be cast to java.lang.String 展开
通过接口的形式得到一组json数据如下:
{。
"name": "小名","age":19,"subTradeList": [。
{。
"balance": "2083.63","money": "0.01","tradeDesc": "工资","tradeTime": "2018-12-07 11:51:01","tradeType": "1"。
"queryTime": "2018-12-07 15:33:07","recAccount": "410350248160111","recAccountName": "(特约)中金支付(资金结算)","tradeAddress": null。
},{。
"balance": "2083.63","money": "0.01","tradeDesc": "代付","tradeTime": "2018-12-07 11:50:58","tradeType": "1"。
"queryTime": "2018-12-07 15:33:07","recAccount": "410350248160111","recAccountName": "(特约)中金支付(资金结算)","tradeAddress": null。
}]}。
像我这样的应该怎么转?
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]));
}
net.sf.json.JSONArray ja=net.sf.json.JSONArray.fromObject(list);
或者
net.sf.json.JSONObject jo=net.sf.json.JSONObject.fromObject(list);
你这个是转成Object类型呢?
jo.toString() 不就是String类型了,而且我这个是json类型 不是obj类型