Skip to content

Commit 64c348b

Browse files
JsonUtil转换json和对象
1 parent 1d7beb6 commit 64c348b

10 files changed

+201
-0
lines changed

.idea/libraries/Maven__commons_beanutils_commons_beanutils_1_8_3.xml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/Maven__commons_collections_commons_collections_3_2_1.xml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/Maven__commons_lang_commons_lang_2_5.xml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/Maven__commons_logging_commons_logging_1_0_4.xml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/Maven__net_sf_ezmorph_ezmorph_1_0_6.xml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/Maven__net_sf_json_lib_json_lib_jdk15_2_4.xml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Utils.iml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
1616
<orderEntry type="library" name="Maven: dom4j:dom4j:1.6.1" level="project" />
1717
<orderEntry type="library" name="Maven: xml-apis:xml-apis:1.0.b2" level="project" />
18+
<orderEntry type="library" name="Maven: net.sf.json-lib:json-lib:jdk15:2.4" level="project" />
19+
<orderEntry type="library" name="Maven: commons-lang:commons-lang:2.5" level="project" />
20+
<orderEntry type="library" name="Maven: commons-beanutils:commons-beanutils:1.8.3" level="project" />
21+
<orderEntry type="library" name="Maven: commons-collections:commons-collections:3.2.1" level="project" />
22+
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.0.4" level="project" />
23+
<orderEntry type="library" name="Maven: net.sf.ezmorph:ezmorph:1.0.6" level="project" />
1824
<orderEntry type="library" name="Maven: log4j:log4j:1.2.17" level="project" />
1925
<orderEntry type="library" name="Maven: org.apache.commons:commons-lang3:3.7" level="project" />
2026
<orderEntry type="library" name="Maven: commons-io:commons-io:2.4" level="project" />

pom.xml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,37 @@
2828
<version>1.6.1</version>
2929
</dependency>
3030

31+
<dependency>
32+
<groupId>net.sf.json-lib</groupId>
33+
<artifactId>json-lib</artifactId>
34+
<version>2.4</version>
35+
<classifier>jdk15</classifier>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>commons-beanutils</groupId>
40+
<artifactId>commons-beanutils</artifactId>
41+
<version>1.8.3</version>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>commons-collections</groupId>
46+
<artifactId>commons-collections</artifactId>
47+
<version>3.2.1</version>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>commons-logging</groupId>
52+
<artifactId>commons-logging</artifactId>
53+
<version>1.0.4</version>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>net.sf.ezmorph</groupId>
58+
<artifactId>ezmorph</artifactId>
59+
<version>1.0.6</version>
60+
</dependency>
61+
3162
<dependency>
3263
<groupId>log4j</groupId>
3364
<artifactId>log4j</artifactId>

src/main/java/com/JsonUtils.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com;
2+
3+
import net.sf.json.JSONArray;
4+
import net.sf.json.JSONObject;
5+
6+
import java.util.List;
7+
8+
/**
9+
* Created by lenovo on 三月
10+
*/
11+
public class JsonUtils {
12+
/**
13+
* 将对象转化为json文本
14+
* @param object
15+
* @param <T>
16+
* @return
17+
*/
18+
public static <T> String objectToJson(Object object){
19+
//将对象转化为Json数组
20+
JSONObject jsonObject= JSONObject.fromObject(object);
21+
return jsonObject.toString();
22+
}
23+
24+
/**
25+
* 将List对象序列化为JSON文本
26+
* @param list
27+
* @param <T>
28+
* @return
29+
*/
30+
public static <T> String listToJson(List<T> list) {
31+
JSONArray jsonArray = JSONArray.fromObject(list);
32+
return jsonArray.toString();
33+
}
34+
35+
36+
/**
37+
* 将Json对象转换为相关类型的对象
38+
* @param jsonObject
39+
* @param beanClass
40+
* @return
41+
*/
42+
public static Object jsonToObject(JSONObject jsonObject,Class beanClass){
43+
return JSONObject.toBean(jsonObject,beanClass);
44+
}
45+
46+
}

src/main/java/com/bean/Person.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.bean;
2+
3+
/**
4+
* Created by lenovo on 三月
5+
*/
6+
public class Person {
7+
private String name;
8+
private String sex;
9+
private int age;
10+
11+
public Person(String name, String sex, int age) {
12+
this.name = name;
13+
this.sex = sex;
14+
this.age = age;
15+
}
16+
17+
public String getName() {
18+
return name;
19+
}
20+
21+
public void setName(String name) {
22+
this.name = name;
23+
}
24+
25+
public String getSex() {
26+
return sex;
27+
}
28+
29+
public void setSex(String sex) {
30+
this.sex = sex;
31+
}
32+
33+
public int getAge() {
34+
return age;
35+
}
36+
37+
public void setAge(int age) {
38+
this.age = age;
39+
}
40+
}

0 commit comments

Comments
 (0)