com.alibaba.fastjson.JSONArray cannot be cast to java.lang.String

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了com.alibaba.fastjson.JSONArray cannot be cast to java.lang.String相关的知识,希望对你有一定的参考价值。

代码:public Object track()
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。

]。

参考技术A billTrackingInfo这是个list,无法直接用json转成string,肯定不对的。追问

像我这样的应该怎么转?

参考技术B 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]));
参考技术C list转json么?可以用net.sf.json包的只要一个步骤;
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类型

参考技术D BillTrackingInfo里面有list变量吗?

以上是关于com.alibaba.fastjson.JSONArray cannot be cast to java.lang.String的主要内容,如果未能解决你的问题,请参考以下文章

FastJSON 常用操作

Json使用之FastJson

怎么把json字符串转换map

阿里的json处理--fastjson

fastjson

Fastjson常用方法