web服务端使用JSONArrayy或JSONObject转json字符串为Java对象或数组
来源:互联网 发布:understand mac 编辑:程序博客网 时间:2024/06/05 09:12
由于项目中需要使用webSocket向各个组件传递json消息格式如[{"parameter":JSON.stringify("需要发送的消息"),"targetId":"目标webSocket的id"}]
,
在服务端将json数据转换为对象数组,对每个对象获取“parameter”以及”targetId”。然后遍历websocket集合查找与”targetId”相同的目标发送”parameter”。
当中转json字符串为Java对象或数组需要使用JSONArrayy或JSONObject,利用maven导jar包时可用json-lib
<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib --><dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier></dependency>
要利用这个jar包时需要依赖另外5个jar包如下
<!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils --><dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.8.0</version></dependency><!-- https://mvnrepository.com/artifact/commons-collections/commons-collections --><dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version></dependency><!-- https://mvnrepository.com/artifact/commons-lang/commons-lang --><dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.5</version></dependency><!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --><dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version></dependency><!-- https://mvnrepository.com/artifact/net.sf.ezmorph/ezmorph --><dependency> <groupId>net.sf.ezmorph</groupId> <artifactId>ezmorph</artifactId> <version>1.0.6</version></dependency>
以上各jar包都为必须,版本可更改但需要注意不冲突,如果使用Intellij idea的话可以用Maven Helper插件查看Maven dependency tree或是查找冲突。
测试时自定义Student类如:
public class Student {
//姓名
private String name;
//年龄
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return “Student [name=” + name + “, age=” + age + “]”;
}
测试代码如:
String arrayStr="[{\"name\":\"Song\",\"age\":\"22\"}]";JSONArray jsonArray=JSONArray.fromObject(arrayStr);Object o=jsonArray.get(0);JSONObject jsonObject=JSONObject.fromObject(o);Student stu=(Student)JSONObject.toBean(jsonObject, Student.class);System.out.println("stu:"+stu);
输出结果:stu:Student [name=Song, age=22]
当时陷入了一个小坑,当代码采用Java Application跑时正确无误,但是当采用Tomcat Server跑时则异常终止,报错如:
18-Jul-2017 23:31:53.326 严重 [http-nio-8081-exec-7] org.apache.tomcat.websocket.pojo.PojoEndpointBase.onError No error handling configured for [websockets.MyWebSocket] and the following error occurred java.lang.RuntimeException: org/apache/commons/lang/exception/NestableRuntimeException
解决办法是根据“org/apache/commons/lang/exception/NestableRuntimeException”将当中maven下的commons-lang-2.5.jar复制到tomcat下lib目录中测试运行如果还有报错则继续复制相关jar包。
有用的话点个赞呗。
- web服务端使用JSONArrayy或JSONObject转json字符串为Java对象或数组
- iOS对象(字典或数组)转化为JSon字符串
- iOS对象(字典或数组)转化为JSon字符串
- 使用Java JSONObject操作JSON字符串对象
- js 转json格式的字符串为对象或数组(前后台)
- Json—使用gson jar包将json字符串解析为java对象或集合(一)
- Java解析Json字符串--数组或列表
- json对象、json格式字符串、java对象【使用此net.sf.json.JSONObject将java对象[比如数组集合Map、Map扩展]转换成json格式字符串】
- JSON字符串转化为JSONOBJECT对象
- java集合或对象转化为json数组或者对象的方法
- iOS对象(字典或数组)转化为JSon…
- jquery转换json对象为字符串JSON.stringify(jsonObject)
- 使用FastJSON,将对象或数组和JSON串互转
- 使用FastJSON,将对象或数组和JSON串互转
- 使用FastJSON,将对象或数组和JSON串互转
- 使用FastJSON,将对象或数组和JSON串互转
- 使用FastJSON,将对象或数组和JSON串互转
- 使用FastJSON,将对象或数组和JSON串互转
- 简易的图片缓存类
- Android Studio Build 错误
- PDF.js使用心得
- matplotlib 图片
- 用maven构建web项目
- web服务端使用JSONArrayy或JSONObject转json字符串为Java对象或数组
- 1 Ubuntu系统下的CUDA安装与配置
- POJ 1583 Robot Motion(dfs+模拟)
- Linux命令笔记(未完成)
- using sap businessobjects predictive analytics with sap hana express edition
- LeetCode 104. Maximum Depth of Binary Tree
- Duplicate files copied in APK META-INF/DEPENDENCIES File
- JavaScript 之 Object.apply()与Object.call()和Object.bind()
- mui html5plus.ready事件