Created
December 3, 2013 07:37
-
-
Save tdoly/7765405 to your computer and use it in GitHub Desktop.
json-lib的使用:可以将beans,maps,collections,java arrays,和XML转换为JSON。同样也可以将JSON转换为beans和DynaBeans。
jackson:很快的JSON解析
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tdoly.java.test.jsonlib; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
import com.tdoly.java.test.beans.User; | |
import com.tdoly.java.test.beans.User.Gender; | |
import net.sf.json.JSONArray; | |
import net.sf.json.JSONObject; | |
public class TestJavaToJson { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
TestJavaToJson test=new TestJavaToJson(); | |
print(test.mapToJSON()); | |
print(test.beansToJSON()); | |
print(test.arrayToJSON()); | |
print(test.listToJSON()); | |
} | |
/** | |
* Map的key值需要为String类型 建议:在使用Map时,最好key值为String类型 原因:String 类型是不可变的,在创建的时候hashcode会被缓存,不需要重新计算。所以会比其他类型的key要快 | |
* @return | |
*/ | |
private JSONObject mapToJSON() { | |
Map<String, Integer> ids=new HashMap<String, Integer>(); | |
ids.put("12", 1); | |
ids.put("13", 1); | |
ids.put("56", 0); | |
ids.put("78", 1); | |
JSONObject json=JSONObject.fromObject(ids); | |
return json; | |
} | |
/** | |
* beans转换为JSON | |
* @return | |
*/ | |
private JSONObject beansToJSON() { | |
User.Name name=new User.Name(); | |
name.setFirst("li"); | |
name.setLast("mingdong"); | |
User user=new User(); | |
user.setId(1L); | |
user.setName(name); | |
user.setGender(Gender.MALE); | |
JSONObject json=JSONObject.fromObject(user); | |
return json; | |
} | |
/** | |
* array转换为JSON | |
* @return | |
*/ | |
private JSONArray arrayToJSON() { | |
String[] s={"a", "b", "c"}; | |
JSONArray json=JSONArray.fromObject(s); | |
return json; | |
} | |
/** | |
* list转换为JSON | |
* @return | |
*/ | |
private JSONArray listToJSON() { | |
List<Object> ls=new ArrayList<Object>(); | |
ls.add("string"); | |
ls.add(new String[]{"array1", "array2", "array3"}); | |
ls.add(1); | |
JSONArray json=JSONArray.fromObject(ls); | |
return json; | |
} | |
private static void print(Object o) { | |
System.out.println(o); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tdoly.java.test.jsonlib; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import org.apache.commons.io.IOUtils; | |
import com.tdoly.java.test.beans.User; | |
import net.sf.json.JSONObject; | |
import net.sf.json.JSONSerializer; | |
import net.sf.json.util.EnumMorpher; | |
import net.sf.json.util.JSONUtils; | |
public class TestJsonToJava { | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
TestJsonToJava test=new TestJsonToJava(); | |
String json=test.readJSON(); | |
test.jsonToBean(json); | |
} | |
/** | |
* json转换为Beans,如果bean中有Enum类型,需要注册Morpher(变换器) | |
* @param json | |
*/ | |
private void jsonToBean(String json) { | |
JSONObject jsonObject=(JSONObject)JSONSerializer.toJSON(json); | |
JSONUtils.getMorpherRegistry().registerMorpher(new EnumMorpher(User.Gender.class)); | |
User user=(User)JSONObject.toBean(jsonObject, User.class); | |
print(user); | |
print(user.getId()); | |
print(user.getName().getFirst()); | |
print(user.getName().getLast()); | |
print(user.getGender()); | |
} | |
private String readJSON() { | |
InputStream is=(InputStream)TestJsonToJava.class.getResourceAsStream("user.json"); | |
String json=""; | |
try { | |
json=IOUtils.toString(is); | |
} catch(IOException e) { | |
e.printStackTrace(); | |
} | |
return json; | |
} | |
private static void print(Object o) { | |
System.out.println(o); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tdoly.java.test.beans; | |
import java.io.Serializable; | |
public class User implements Serializable { | |
public static class Name { // 姓,名 | |
private String first, last; | |
public String getFirst() { | |
return first; | |
} | |
public void setFirst(String first) { | |
this.first=first; | |
} | |
public String getLast() { | |
return last; | |
} | |
public void setLast(String last) { | |
this.last=last; | |
} | |
} | |
public enum Gender { // 性别 | |
MALE(1, "男"), FEMAIL(0, "女"); | |
private Integer id; | |
private String detail; | |
private Gender(Integer id, String detail) { | |
this.id=id; | |
this.detail=detail; | |
} | |
public Integer getId() { | |
return id; | |
} | |
public void setId(Integer id) { | |
this.id=id; | |
} | |
public String getDetail() { | |
return detail; | |
} | |
public void setDetail(String detail) { | |
this.detail=detail; | |
} | |
}; | |
private Long id; | |
private Name name; | |
private Gender gender; | |
public Long getId() { | |
return id; | |
} | |
public void setId(Long id) { | |
this.id=id; | |
} | |
public Name getName() { | |
return name; | |
} | |
public void setName(Name name) { | |
this.name=name; | |
} | |
public Gender getGender() { | |
return gender; | |
} | |
public void setGender(Gender gender) { | |
this.gender=gender; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"gender":"MALE", | |
"id":1, | |
"name": | |
{ | |
"first":"li", | |
"last":"mingdong" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment