[Solved] net.sf.json.JSONException: java.lang.ClassCastException: JSON keys must be strings.

 

1. Log:

   Exception in thread "roomSynRoomThreadPool-0--thread-2" net.sf.json.JSONException: java.lang.ClassCastException: JSON keys must be strings.
	at net.sf.json.JSONObject._fromMap(JSONObject.java:1185)
	at net.sf.json.JSONObject.fromObject(JSONObject.java:163)
	at net.sf.json.JSONObject.fromObject(JSONObject.java:134)
	at com.xs.fun.base.remote.RemoteDataUtil.buildWorldCupMatchInfoLogMessage(RemoteDataUtil.java:208)
	at com.xs.fun.worldcup.bo.fight.WorldCupFightRoom.sendBullFightRoleAndMatchLogInfo(WorldCupFightRoom.java:732)
	at com.xs.fun.worldcup.bo.fight.WorldCupFightRoom.update(WorldCupFightRoom.java:165)
	at com.xs.fun.base.bo.fight.FightRoomBase$4$1.processTask(FightRoomBase.java:360)
	at com.ebo.synframework.synroom.executor.SynRoomTask.run(SynRoomTask.java:51)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassCastException: JSON keys must be strings.
	at net.sf.json.JSONObject._fromMap(JSONObject.java:1145)
	... 10 more

2. Solution:

1) After checking the relevant information on the Internet, I found that when a map is placed in jsonobject, it will automatically treat the map as jsonobject. The key value in my map is of integer type. This exception will be reported when converting. Just change the key value in the map to string type
similarly, when writing code in the future, if you encounter the situation of placing map in JSON, you must pay attention to the type of key value in JSON, which can only be string , remember

2) Using setallownonstringkeys to solve Java. Lang. ClassCastException: JSON keys must be strings
problem

3) Others:

“Why JSON allows only string to be a key?”, Answer from segmentfault:

1. In Python, the key of dictionary can be any immutable object, but the key of JSON can only be string. The best answer is that JSON is to transfer data between different programs, so string can ensure that different programming languages can support this data structure

2. First of all, JSON is a subset of JavaScript. In JavaScript, there are only five data types, such as string and integer. Then it is used in other programming languages. Other languages are implemented to be compatible with JSON, such as the JSON module of Python standard library. You can see the details of JSON
as for why the string is used as a key in JSON, the main reason is that first of all, it needs to be an immutable object, so it can only be string, integer, floating-point data types. In computer, an integer needs to occupy four bytes of memory, while characters only occupy one byte
in addition, JSON is mainly used for data transmission. In the process of network transmission, the loss of precision also needs to be considered, while integer and floating-point numbers will lose precision
in addition, in the form of string, compression algorithm can be used to compress, thus saving bandwidth

Similar Posts: