`
- 浏览:
109182 次
-
-
1
.
-
-
-
-
-
-
-
8
.
public
static
Object getDTO(String jsonString, Class clazz){
-
9
. JSONObject jsonObject =
null
;
-
10
.
try
{
-
11
. setDataFormat2JAVA();
-
12
. jsonObject = JSONObject.fromObject(jsonString);
-
13
. }
catch
(Exception e){
-
14
. e.printStackTrace();
-
15
. }
-
16
.
return
JSONObject.toBean(jsonObject, clazz);
-
17
. }
-
18
.
-
19
.
-
-
-
-
-
-
-
-
-
28
.
public
static
Object getDTO(String jsonString, Class clazz, Map map){
-
29
. JSONObject jsonObject =
null
;
-
30
.
try
{
-
31
. setDataFormat2JAVA();
-
32
. jsonObject = JSONObject.fromObject(jsonString);
-
33
. }
catch
(Exception e){
-
34
. e.printStackTrace();
-
35
. }
-
36
.
return
JSONObject.toBean(jsonObject, clazz, map);
-
37
. }
-
38
.
-
39
.
-
-
-
-
-
-
-
46
.
public
static
Object[] getDTOArray(String jsonString, Class clazz){
-
47
. setDataFormat2JAVA();
-
48
. JSONArray array = JSONArray.fromObject(jsonString);
-
49
. Object[] obj =
new
Object[array.size()];
-
50
.
for
(
int
i =
0
; i < array.size(); i++){
-
51
. JSONObject jsonObject = array.getJSONObject(i);
-
52
. obj[i] = JSONObject.toBean(jsonObject, clazz);
-
53
. }
-
54
.
return
obj;
-
55
. }
-
56
.
-
57
.
-
-
-
-
-
-
-
-
65
.
public
static
Object[] getDTOArray(String jsonString, Class clazz, Map map){
-
66
. setDataFormat2JAVA();
-
67
. JSONArray array = JSONArray.fromObject(jsonString);
-
68
. Object[] obj =
new
Object[array.size()];
-
69
.
for
(
int
i =
0
; i < array.size(); i++){
-
70
. JSONObject jsonObject = array.getJSONObject(i);
-
71
. obj[i] = JSONObject.toBean(jsonObject, clazz, map);
-
72
. }
-
73
.
return
obj;
-
74
. }
-
75
.
-
76
.
-
-
-
-
-
-
82
.
public
static
List getDTOList(String jsonString, Class clazz){
-
83
. setDataFormat2JAVA();
-
84
. JSONArray array = JSONArray.fromObject(jsonString);
-
85
. List list =
new
ArrayList();
-
86
.
for
(Iterator iter = array.iterator(); iter.hasNext();){
-
87
. JSONObject jsonObject = (JSONObject)iter.next();
-
88
. list.add(JSONObject.toBean(jsonObject, clazz));
-
89
. }
-
90
.
return
list;
-
91
. }
-
92
.
-
93
.
-
-
-
-
-
-
-
100
.
public
static
List getDTOList(String jsonString, Class clazz, Map map){
-
101
. setDataFormat2JAVA();
-
102
. JSONArray array = JSONArray.fromObject(jsonString);
-
103
. List list =
new
ArrayList();
-
104
.
for
(Iterator iter = array.iterator(); iter.hasNext();){
-
105
. JSONObject jsonObject = (JSONObject)iter.next();
-
106
. list.add(JSONObject.toBean(jsonObject, clazz, map));
-
107
. }
-
108
.
return
list;
-
109
. }
-
110
.
-
111
.
-
-
-
-
-
-
-
118
.
public
static
Map getMapFromJson(String jsonString) {
-
119
. setDataFormat2JAVA();
-
120
. JSONObject jsonObject = JSONObject.fromObject(jsonString);
-
121
. Map map =
new
HashMap();
-
122
.
for
(Iterator iter = jsonObject.keys(); iter.hasNext();){
-
123
. String key = (String)iter.next();
-
124
. map.put(key, jsonObject.get(key));
-
125
. }
-
126
.
return
map;
-
127
. }
-
128
.
-
129
.
-
-
-
-
-
-
135
.
public
static
Object[] getObjectArrayFromJson(String jsonString) {
-
136
. JSONArray jsonArray = JSONArray.fromObject(jsonString);
-
137
.
return
jsonArray.toArray();
-
138
. }
-
139
.
-
140
.
-
141
.
-
-
-
-
-
-
-
-
149
.
public
static
String getJSONString(Object object)
throws
Exception{
-
150
. String jsonString =
null
;
-
151
.
-
152
. JsonConfig jsonConfig =
new
JsonConfig();
-
153
. jsonConfig.registerJsonValueProcessor(java.util.Date.
class
,
new
JsonDateValueProcessor());
-
154
.
if
(object !=
null
){
-
155
.
if
(object
instanceof
Collection || object
instanceof
Object[]){
-
156
. jsonString = JSONArray.fromObject(object, jsonConfig).toString();
-
157
. }
else
{
-
158
. jsonString = JSONObject.fromObject(object, jsonConfig).toString();
-
159
. }
-
160
. }
-
161
.
return
jsonString ==
null
?
"{}"
: jsonString;
-
162
. }
-
-
本文来自CSDN博客,转载请标明出处:http:
-
-
需要jar包:ezmorph-1.0
.
6
.jar,json-lib-
2.3
.jar
-
-
创建java对象:
-
-
public
class
Address {
-
private
String road;
-
private
String streate;
-
private
String provience;
-
private
String no;
-
public
String getRoad() {
-
return
road;
-
}
-
public
void
setRoad(String road) {
-
this
.road = road;
-
}
-
public
String getStreate() {
-
return
streate;
-
}
-
public
void
setStreate(String streate) {
-
this
.streate = streate;
-
}
-
public
String getProvience() {
-
return
provience;
-
}
-
public
void
setProvience(String provience) {
-
this
.provience = provience;
-
}
-
public
String getNo() {
-
return
no;
-
}
-
public
void
setNo(String no) {
-
this
.no = no;
-
}
-
}
-
-
1
.将json对象转化为java对象
-
-
JSONObject jsonObject = JSONObject.fromObject("{\"no\":\"104\",\"provience\":\"陕西\",\"road\":\"高新路\",\"streate\":\"\"}"
);
-
Address Address = (Address) JSONObject.toBean(jsonObject,Address.class
);
-
log.info(Address.getNo());
-
log.info(Address.getStreate());
-
log.info(Address.getProvience());
-
log.info(Address.getRoad());
-
-
-
-
2
.将java对象转化为json对象
-
-
将java对象转化为json对象:
-
-
Address address = new
Address();
-
address.setNo("104"
);
-
address.setProvience("陕西"
);
-
address.setRoad("高新路"
);
-
address.setStreate(""
);
-
JSONArray json = JSONArray.fromObject(address);
-
log.info(json.toString());
-
-
-
-
将java对象list转化为json对象:
-
-
Address address = new
Address();
-
address.setNo("104"
);
-
address.setProvience("陕西"
);
-
address.setRoad("高新路"
);
-
address.setStreate(""
);
-
Address address2 = new
Address();
-
address2.setNo("105"
);
-
address2.setProvience("陕西"
);
-
address2.setRoad("未央路"
);
-
address2.setStreate("张办"
);
-
List list = new
ArrayList();
-
list.add(address);
-
list.add(address2);
-
JSONArray json = JSONArray.fromObject(list);
-
log.info(json.toString());
-
-
3
.JSONArray转化为list
-
-
JSONObject jsonObject = JSONObject.fromObject("{\"no\":\"104\",\"provience\":\"陕西\",\"road\":\"高新路\",\"streate\":\"\"}"
);
-
JSONArray jsonArray = new
JSONArray();
-
jsonArray.add("{\"no\":\"104\",\"provience\":\"陕西\",\"road\":\"高新路\",\"streate\":\"\"}"
);
-
jsonArray.add("{\"no\":\"104\",\"provience\":\"陕西\",\"road\":\"高新路\",\"streate\":\"123\"}"
);
-
Object object = JSONArray.toList(jsonArray,Address.class
);
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
解决net.sf.json.JSONObject 解决net.sf.json.JSONObject
JSON net.sf.json 依赖的jar包 亲测可用 一次导入所有jar包
net.sf.json.JSONObject需要的jar包 -- net.sf.json.JSONObject需要的jar包
net.sf.json.JSONObject所需要的jar包 commons-beanutils-1.9.3.jar commons-collections-3.2.2 commons-lang-2.6 commons-logging-1.2 ezmorph-1.0.6 json-lib-2.4-jdk15
Java中使用Json的net.sf.json包,其中包含JSONObject与JSONArray等操作类,十分好用。
net.sf.json.JSONObject相关依赖架包,导入项目后,JSONObject.fromObject()方法就可以使用了
net.sf.json.JSONArray等JSON类所需要的jar包组合。包括JSONObject等
net.sf.json.JSONObject相关jar包
Java的net.sf.json.JSONObject , 发现Maven仓库找不到, 特意上传供大家分享
net.sf.json依赖jar包,其中包括: commons-beanutils-1.7.0.jar commons-collections-3.1.jar commons-lang-2.5.jar commons-logging.jar ezmorph-1.0.3.jar json-lib-2.4-jdk15.jar
net.sf.json需要的jar
net.sf.json使用的lib(包括依赖lib) 1.commons-lang.jar 2.commons-beanutils.jar 3.commons-collections.jar 4.commons-logging.jar 5.ezmorph.jar 6.json-lib-2.2.2-jdk15.jar
net.sf.json jar包包含了一下:可以用来解析json,具体操作可以看小编的博客。 commons-lang.jar commons-beanutils.jar commons-collections.jar commons-logging.jar ezmorph.jar json-lib-2.2.2-jdk15.jar
java开发中需要jsonObject,jsonArray需要用到6个jar包,net.sf.json.JSONObject commons-beanutils-1.7.0.jar commons-collections-3.1.jar commons-lang-2.5.jar commons-logging.jar ezmorph-1.0.3.jar json-lib-...
在用到net.sf.json的时候有时候会因为缺少依赖库而报错,此时可以试试这个包
阿里fastjson和谷歌net.sf.json(三种)
将java对象转为JS对象,可使用net.sf.json.JSONObject对象,将Java对象转成JSONObject对象,该对象的toString方法就是JSON对象的字符串表现形式
net.sf.json jar包含以下: commons-lang.jar commons-beanutils.jar commons-collections.jar commons-logging.jar ezmorph.jar json-lib-2.2.2-jdk15.jar
改json jar包,有6个资源包,在实际项目中亲测,大家可以放心使用!
开源最新的json 库文件。json-lib-2.4-jdk15, fastjson-1.2.2