There is a StockData object. Normally, the JSON string to object should be:
StockData stockData = JSONObject.parseObject(str, StockData.class);
However, if the object is a generic (e.g. StockData<StockDetail>), it can be converted as above, but StockDetail throws an exception when it gets the object parameters via get com.alibaba.fastjson. JSONObject cannot be cast to cn.seagen.sorting.bean.StockDetail.
StockData<StockDetail> stockData = JSONObject.parseObject(str, StockData.class);
prompt JSONObject can not be converted to StockDetail object, the reason is probably.
fastjson conversion json object encounters a generic, it will not be correctly converted to a generic object, the converted object is a JSONObject object, not the object inside the generic, so there is also the above-thrown exception.
Solution
In addition to the above methods, fastjson also overloads a method.
public static <T> T parseObject(String text, TypeReference<T> type, Feature... features){}
Therefore, when the string is converted to a generic object, it is OK to use TypeReference for conversion. After conversion, StockDetail can normally get the parameter value of the object.
StockData<StockDetail> stockData = JSONObject.parseObject(str, new TypeReference<StockData<StockDetail>>(){});
Read More:
- Request processing failed; nested exception is com.alibaba.fastjson.JSONException: can not cast to J
- How to Solve ERROR – unregister mbean error javax.management.InstanceNotFoundException: com.alibaba.druid:type=
- [Solved] com.alibaba.druid.pool.DruidDataSource – create connecti
- [Solved] javax.management.InstanceNotFoundException: com.alibaba.druid:type=DruidDataSourceStat
- Calling Dubbo Service reported the following error (com.alibaba.dubbo.remoting.RemotingException) Causes and Solutions
- com.alibaba.druid.pool.DruidDataSource error Warning: init datasource error
- IDEA Error: org.jetbrains.jps.builders.java.dependencyView.TypeRepr$PrimitiveType cannot be cast t
- net.sf.json .JSONObject maven20381;- 36182;
- [Solved] Manifest merger failed: android:exported needs to be explicitly specified for element <activity#com
- Clickhouse error: XXXX.XXXX_local20211009 (8fdb18e9-bb4c-42d8-8fdb-18e9bb4c02d8): auto…
- std::dynamic_pointer_cast Error: source type is not polymorphic
- [Solved] Error: Main class not found or could not be loaded com.jawasoft.
- [Solved] Installing github.com/uudashr/gopkgs/v2/cmd/gopkgs@latest FAILED和cannot install, GOBIN must be an ab
- [Solved] failed to set bridge addr: “cni0“ already has an IP address different from xxxx
- [Solved] IDEA Error: Unable to resolve column XXXX
- How to Solve Error: spawn xxxx ENOENT
- [Solved] VScode powershell Run ts-node Error: Unable to load file xxxx…
- [Solved] Hbase …ERROR: Unable to read .tableinfo from file:/hbaseData/data/default/table1/xxxx
- The showdialog() method in thread/threading. Timer/task reported an error: “before ole can be called, the current thread must be set to single thread unit (STA) mode.”
- How to Fix Error when integrating spring cloud openfeign with spring cloud Alibaba