string 转换成json
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了string 转换成json相关的知识,希望对你有一定的参考价值。
我在前台传入了一个劲松对象var
UserObject =
"user" : "aa";
;
传到后台的时候是“user”:"aa"
我用JSONObject的fromObject(json);来将这个字符串转换为Json对象,
但是总是报Caused by: java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException
我是没有办法了,请帮帮忙,谢谢
package com.satoteam.action;
import java.util.ArrayList;
import net.sf.json.JSONObject;
public class LoginAction
private String json;
//json="user":"aa"
public void setJson(String json)
System.out.println(json);
JSONObject jo = JSONObject.fromObject(json);
System.out.println("11111"+jo.getString("user"));
this.json = json;
我用的是json-lib-2.4-jdk15.jar
报的错
2011-5-6 9:39:57 com.opensymphony.xwork2.util.logging.jdk.JdkLogger warn
警告: Error setting expression 'json' with value '[Ljava.lang.String;@e3404f'
ognl.MethodFailedException: Method "setJson" failed for object com.satoteam.action.LoginAction@1204425 [java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException]
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException
/-- Encapsulated exception ------------\
java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException
From Beans to JSON:
class MyBean
private String name = "json";
private int pojoId = 1;
private char[] options = new char[];
private String func1 = "function(i)";
private JSONFunction func2 = new JSONFunction(new String[],"return this.options[i];");
// getters & setters
...
JSONObject jsonObject = JSONObject.fromObject( new MyBean() );
System.out.println( jsonObject );
/* prints
"name":"json","pojoId":1,"options":["a","f"],
"func1":function(i),
"func2":function(i)
*/
在你这里就是:JSONObject jsonObject = JSONObject.fromObject(user );
From JSON to Beans:
String json = "";
JSONObject jsonObject = JSONObject.fromObject( json );
BeanA bean = (BeanA) JSONObject.toBean( jsonObject, BeanA.class );
在你这里就是:
JSONObject jsonObject = JSONObject.fromObject( json );
User bean = (User) JSONObject.toBean( jsonObject, User.class );
另外,站长团上有产品团购,便宜有保证 参考技术B 你需要用到第三方的lib文件: json-lib。
From Beans to JSON:
class MyBean
private String name = "json";
private int pojoId = 1;
private char[] options = new char[];
private String func1 = "function(i)";
private JSONFunction func2 = new JSONFunction(new String[],"return this.options[i];");
// getters & setters
...
JSONObject jsonObject = JSONObject.fromObject( new MyBean() );
System.out.println( jsonObject );
/* prints
"name":"json","pojoId":1,"options":["a","f"],
"func1":function(i),
"func2":function(i)
*/
在你这里就是:JSONObject jsonObject = JSONObject.fromObject(user );
From JSON to Beans:
String json = "";
JSONObject jsonObject = JSONObject.fromObject( json );
BeanA bean = (BeanA) JSONObject.toBean( jsonObject, BeanA.class );
在你这里就是:
JSONObject jsonObject = JSONObject.fromObject( json );
User bean = (User) JSONObject.toBean( jsonObject, User.class );追问
我用的是struts2的json-lib但是一直报错,是为什么呢
参考技术C 虽然时间已经过去那么就了,但我还是回答,这里的警告是说,json参数的类型是String ,从这段话中,我可以猜到,JSON是用反射的,获取对象的set访问器,也就是说这里的错误就是:参数JSON必须时一个实体 bean ,才能使用 参考技术D 少了 commons-long.jar包将list<String>转换成StringBuffer,再把json转换成pojo类
List<String> list = FileUtils.conventStreamToStr(byteArrayInputStream, -1, "UTF-8");
log.info("list=",list);
StringBuffer stringBuffer = new StringBuffer();
if(!CollectionUtils.isEmpty(list))
list.stream().forEach(l->stringBuffer.append(l));
// 将json转换为类
PjtTplateInfo pjtTplateInfo = JsonUtils.readValue(stringBuffer.toString(), PjtTplateInfo.class);
以上是关于string 转换成json的主要内容,如果未能解决你的问题,请参考以下文章