FAQ(84):net.sf.json.JSONException: java.lang.ClassCastException: JSON keys must be strings.

步进电机 阅读详情

2018年8月31日

1、日志:

   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、解决方法:

1)去网上查了下相关资料,才发现JSONObject中放置Map的时候,会自动将Map看成是JSONObject来处理,而我的map中的key值是Integer类型的,在转化的时候就会报这个异常,将Map中的key值换成String类型即可。 
同理,以后写代码时,如果碰到JSON中放置Map的情况时,一定要注意json中的key值的类型,只能是String,切记切记!

2)通过setAllowNonStringKeys解决java.lang.ClassCastException: JSON keys must be strings
 

3)其他:

“在stackoverflow上搜到的相关问题,Why JSON allows only string to be a key?”,来自segmentfault的回答:

1、在python中,字典的key可以是任意immutable对象,但json的key却只能是string。最佳答案是说json是为了数据在不同程序之间相互传递,所以string能保证不同的程序语言都能支持这种数据结构。

2、首先JSON是Javascript的子集,而在Javascript中只有字符串、整型等5个数据类型,而后在其他编程语言中被使用,其他的语言的实现都是为了兼容JSON,比如Python标准库的json模块。详情可以查看json 
至于JSON中为什么采用字符串作为键主要在于,首先需要是1个不可变的对象,因此只能是字符串、整型、浮点型这样的数据类型了,而在计算机中1个整型需要占用4个字节的内存,而字符只占用1个字节。 
另外,使用JSON主要是为了传输数据,这个网络传输的过程中会丢失精度也是需要考虑的,而整型和浮点数都会出现精度的丢失。 
另外使用字符串的形式还可以使用压缩算法进行压缩,进而节省带宽。 
因此字符串是最佳的选择。

基于英特尔 RealSense D455 相机实现裂缝尺寸以及深度测量 D455 相机可用于对这些零部件进行高精度的表面检测,快速准确地测量裂缝的尺寸和深度,实现自动化的质量控制和缺陷筛查,提高生产效率和产品质量。D455 相机能够对焊缝表面进行细致扫描,测量焊缝的宽度、深度以及焊缝表面的不规则程度等参数,帮助检测焊缝是否存在缺陷,如未焊透、焊瘤等,为焊接质量的评估和改进提供数据支持。:RGB 传感器采用全局快门,并且其视场与深度视场相匹配,能够提供更出色的 RGB 图像质量,以及深度图像和 RGB 图像之间的更好对应性,从而可提高扫描质量。可以看出宽度和高度都是十分准确的。 阅读详情

相关推荐

AD7920实战:从咪头到ADC的音频信号采集电路设计全解析(附电路图)

本文详细解析了基于AD7920 ADC芯片的音频信号采集电路设计全流程。从驻极体咪头的驱动、AD8542运放的两级增益与偏置设计,到关键的抗混叠滤波器配置,以及ADC接口的驱动缓冲与PCB布局要点,提供了完整的实战指南与电路图,帮助开发者实现高质量音频信号采集。

red88的博客 799

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

我的需求是:在把字符串转成map类型后,放入net.sf.json.JSON。但是在测试的时候,这个地方提示报错。由此在网上各种搜资料,查询出问题的原因图中红框中就是报错的位置。

qq_28421487的博客 496

ADS中HB仿真自动优化报错问题解决心得

在ADS HB仿真中自动优化出现报错被强制暂停,解决方法如下

yyyyyyyyyyjy的博客 6537

异常信息:java.lang.ClassCastException: JSON keys must be strings.

异常信息: Exception in thread "main" java.lang.ClassCastException: JSON keys must be strings. at net.sf.json.JSONObject._fromJSONObject(JSONObject.java:1099) at net.sf.json.JSONObject.fromObject(JSONObject.java:151) at net.sf.json.JSONSerializer.toJSON(JSON

qq_42449963的博客 918

【C++】POCO学习总结(六):线程、线程池、同步

Poco::RunnableAdapter继承自Poco::Runnable,在Poco::RunnableAdapter构造函数中指定了类和方法;Poco::ThreadLocal是线程局部变量,也叫线程局部存储,意思是模版ThreadLocal中填充的变量属于当前线程,该变量对其他线程而言是隔离的,也就是说该变量是当前线程独有的变量。

郭老二 5098

用群辉NAS打造影视墙(Jellyfin篇)

之前的文章提到过,用群辉NAS组建影视播放系统可以采用官方提供的Video Station套件,不过影视信息配比较差,响应速度也欠佳,今天我们就聊聊别的套件。在影音媒体方面,常见的还有Emby、Plex、Jellyfin。本文主讲Jellfin的原因是它胜在免费,而且功能比较齐全,完全够用。详细信息可以参考Jellyfin的官网:https://jellyfin.org/

dgnankai的专栏 7323

JSON keys must be strings

代码里一个map1里存了一个key是Integer的map2。在map1入库时,JSONArray json = JSONArray.fromObject(map1); 将json入库时转换报错net.sf.json.JSONException: java.lang.ClassCastException: JSON keys must be strings.大致意思就像下面的:JSONObject...

酵母君的博客 3427

Sigrity System SI Parallel Bus Analysis模式进行DDR3仿真分析操作指导-单个颗粒

Default Strobe Offset:DQS默认偏移时间,自动填充为0.5ns,即DQS默认偏移1/2UI,DQS滞后于DQ 1/2UI,让建立保持时间均分。WLO:写平衡偏移的数值,DataL是0ps,DataU是500ps,是根据WLOSkew自动定义的,可以认为是WLOSkew取整,也可以根据需要手动调整数值。注意AddCmd的UI=T,这里的Data Rate仍然是DQ的速率,并不是地址线的速率,地址线的速率=1/UI=1/2ns=0.5Gbps。

weixin_54787054的博客 366

通过setAllowNonStringKeys解决java.lang.ClassCastException: JSON keys must be strings

我的解决方案是

tiantangpw的专栏 1234

R语言caret拟合模型:train函数构建xgBoost Dart模型、模型调优、自定义设置trainControl函数和tuneLength参数

R语言caret拟合模型:train函数构建xgBoost Dart模型、模型调优、自定义设置trainControl函数和tuneLength参数

statistics+insight+vista+power 530

YOLOv10改进 | 主干篇 | 引入FasterNet:Run, Don‘t Walk,追求更快的神经网络

本文引入了 FasterNet 作为 YOLOv10 的主干网络。通过 PConv 算子,我们有效降低了模型的计算冗余。对于追求极致推理速度的场景(如树莓派、Jetson Nano),该改进方案极具价值。建议在训练时配合混合精度训练 (AMP)进一步加速。

weixin_51988935的博客 604

对JSON数据进行解析,很实用的工具类

package com.utils;import java.io.File;import java.io.IOException;import java.io.PrintWriter;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.M...

吴举人的博客 1094

一分钟了解 “提示词工程”、“上下文工程”、“驾驭工程”

在大模型刚刚进入大众视野的初期,开发者面临的最大挑战是如何让模型听懂人类的指令。这一阶段被称为提示词工程时代。其核心思想非常直观:通过精心设计输入的文本提示,引导模型输出符合预期的结果。这一时期的开发者更像是“提示词巫师”,他们热衷于寻找所谓的“魔法咒语”。典型的技巧包括角色设定、思维链、少样本学习以及输出格式约束。例如,仅仅在问题前加上一句“请一步步思考”,模型的推理能力往往就能得到显著提升。然而,随着应用场景的复杂化,提示词工程的局限性逐渐暴露。

h a 6 6 6 c k 为 你解决复杂难题! 568

net.sf.json包的json使用时,报JSON keys must be Strings的问题

在spring boot的项目中,由于二次序列化,fastjson解析数据落盘至HDFS路径时,会造成反斜杠\ 换用net.sf.json.json可以解决这个问题 但是net.sf.json.json对于两种json解析不成功,一种是key为"null"的情况,另一种是key中存在\“XXX”反斜杠导致解析不成功的情况 报错如下: 具体解析使用的是 val json = JSON...

攻城狮Kevin 1137

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

执行: JSONArray array = JSONArray.fromObject(this.users); 就会报以下错误: net.sf.json.JSONException: java.lang.reflect.InvocationTargetException users是一个list集合 方案一: JSONArray array = JSONArray.fr

ye1992的专栏 9万+

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

使用net.sf.json包生成一个pojo的json数据出现的问题

今天在使用一个net.sf.json这个包中的类来生成一个pojo的json数据,总是出现一个net.sf.json.JSONException: java.lang.ClassCastException: net.sf.json.JSONObjectcannot be cast to java.lang.String错误,刚开始就纳闷,怎么会出现情况啊。最后我发现,难道是我的get个set方

jimmee的专栏 6436

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

json解析异常 - net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

json解析异常 - net.sf.json.JSONException: java.lang.reflect.InvocationTargetException

runwuwushengxiyu的博客 288

java.lang.ClassCastException : net.sf.cglib............................................异常

我们公司的 weblogic 服务器上今天新增加部署了一个 web 项目,部署,配置,一系列调通,启动正常。这时候原来的web项目 有新代码需要部署更新,在部署上去,更新启动时,就一直报错    java.lang.reflect.InvocationTargeException  ........         case by : java.lang.ExceptionInInitialize...

qq_33468062的博客 1325

json中key must be strings, unquoted错误

json中key 必须是 string类型,包括JSONObject和JSONArray(即由一个string对应到array) 否则会抛异常,如标题所示 我之前遇到了这个问题,错在两个地方 1. HashMap HashMap对象中 key 使用的是 Integer,所以 JSONObject.fromObject()抛异常,告知key must be strings,...

wuwei35531的专栏 1501
上一篇: FAQ(83):### Cause: java.sql.SQLException: The table 't_account_change_6' is full
下一篇: FAQ(85):java.sql.BatchUpdateException: Data truncation: Data too long for column
后台技术汇
博客等级 码龄11年 670粉丝 531原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

后台技术汇

对你的帮助,是对我的最好鼓励。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值