java实体类怎么转换成json。 java怎么把数据转成json 数组
Java\u5b9e\u4f53\u7c7b\u8f6c\u6362\u6210json\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5 \u6700\u597d\u544a\u8bc9\u6211\u9700\u8981\u54ea\u4e9bjar\u8fd9\u91cc\u8bb2\u7684\u5f88\u6e05\u695a\u3002
http://www.sojson.com/blog/100.html
\u6709\u4e24\u79cd\u65b9\u6cd5\u5b9e\u73b0\uff1a
\uff081\uff09\u76f4\u63a5\u8f6c\u6362\uff1a
\u521b\u5efaJSONArray\u5bf9\u8c61\u3001JSONObject\u5bf9\u8c61\uff0c\u901a\u8fc7put\u65b9\u6cd5\u5c06\u5176\u6570\u636e\u4ee5\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u586b\u5145\uff0c\u5982\u4f8b\uff1a
//\u5047\u5982\u6709\u4e00\u4e2aJava\u5b9e\u4f53\u7c7bPerson\uff0c\u5305\u62ec\u4ee5\u4e0b\u5c5e\u6027\uff1aphone\u3001sex\u3001married\u3001address\u3002
ArrayList persons = new ArrayList();
for (int i = 0; i < 3; i++) {
persons.add(new Person("138******67", "\u5973", true, "\u5317\u4eac\u671d\u9633\u533a"));//\u586b\u5145Java\u5b9e\u4f53\u7c7b\u96c6\u5408
}
JSONArray array = new JSONArray();// Json\u683c\u5f0f\u7684\u6570\u7ec4\u5f62\u5f0f
JSONObject obj;//json\u683c\u5f0f\u7684\u5355\u4e2a\u5bf9\u8c61\u5f62\u5f0f
for (int i = 0; i < persons.size(); i++) {
obj = new JSONObject();
//\u4f9d\u6b21\u586b\u5145\u6bcf\u4e00\u4e2aJava\u5b9e\u4f53\u5bf9\u8c61
try {
obj.put("phone", persons.get(i).getPhone());//json\u901a\u8fc7put\u65b9\u5f0f\u4ee5key-value\u5f62\u5f0f\u586b\u5145
obj.put("sex", persons.get(i).getSex());
obj.put("married", persons.get(i).getMarried());
obj.put("address", persons.get(i).getAddress());
array.put(obj);//\u5c06JSONObject\u6dfb\u52a0\u5165JSONArray
} catch (JSONException e) {
e.printStackTrace();
}
}
\uff082\uff09\u901a\u8fc7GSON\u5de5\u5177\u5b9e\u73b0\uff1a
\u53ea\u9700\u8981\u521b\u5efaGSON\u5bf9\u8c61\uff0c\u901a\u8fc7toJson\u65b9\u6cd5\u8f6c\u6362\u5373\u53ef\u3002\u5982\u4f8b\uff1a
//\u6a21\u62dfJava\u6570\u636e
Person person = new Person("\u654f\u654f", 23, "\u5973", "\u5317\u4eac\u5e02\u6d77\u6dc0\u533a");
//\u521b\u5efaGSON\u5bf9\u8c61
Gson gson = new Gson();
//\u8c03\u7528toJson\u65b9\u6cd5\u5373\u53ef\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684json\u683c\u5f0f
String json = gson.toJson(person);
然后实例化Gson
static Gson gosn = new Gson();
String json = gosn.toJson(hashMap); //这里放一个对象,什么对象都可以。
转化后就是Json,功能强大很多,也简单很多。
json-lib-2.4-jdk15.jar
ezmorph-1.0.6.jar
转换的话这样用
String s= JSONArray.fromObject(user).toString();
spring-webmvc4
在方法上加入@ResponseBody,同时方法返回值为实体对象,spring会自动将对象转换为json格式,并返回到客户端
实例代码:
public class JSONUtil {
/**
* 将json转化为实体POJO
* @param jsonStr
* @param obj
* @return
*/
public static<T> Object JSONToObj(String jsonStr,Class<T> obj) {
T t = null;
try {
ObjectMapper objectMapper = new ObjectMapper();
t = objectMapper.readValue(jsonStr,
obj);
} catch (Exception e) {
e.printStackTrace();
}
return t;
}
/**
* 将实体POJO转化为JSON
* @param obj
* @return
* @throws JSONException
* @throws IOException
*/
public static<T> JSONObject objectToJson(T obj) throws JSONException, IOException {
ObjectMapper mapper = new ObjectMapper();
// Convert object to JSON string
String jsonStr = "";
try {
jsonStr = mapper.writeValueAsString(obj);
} catch (IOException e) {
throw e;
}
return new JSONObject(jsonStr);
}
public static void main(String[] args) throws JSONException, IOException {
JSONObject obj = null;
obj = new JSONObject();
obj.put("name", "213");
obj.put("age", 27);
JSONArray array = new JSONArray();
array.put(obj);
obj = new JSONObject();
obj.put("name", "214");
obj.put("age", 28);
array.put(obj);
Student stu = (Student) JSONToObj(obj.toString(), Student.class);
JSONObject objList = new JSONObject();
objList.put("student", array);
System.out.println("objList:"+objList);
StudentList stuList = (StudentList) JSONToObj(objList.toString(), StudentList.class);
System.out.println("student:"+stu);
System.out.println("stuList:"+stuList);
System.out.println("#####################################");
JSONObject getObj = objectToJson(stu);
System.out.println(getObj);
}
}
绛旓細涓嬭浇鍦板潃:http://json-lib.sourceforge.net/ 杩樿闇瑕佺殑绗3鏂瑰寘:org.apache.commons(3.2浠ヤ笂鐗堟湰)org.apache.oro net.sf.ezmorph(ezmorph-1.0.4.jar)nu.xom 1銆丩ist Java浠g爜 boolean[] boolArray =newboolean[]{true,false,true};JSONArray jsonArray1 = JSONArray.fromObject( boolArray );...
绛旓細JSO鏄疛ABBER娴佸璞(Stream Objects)鐨勪竴涓Java鍖.瀹冧负Jabber/XMPP鍗忚鎻愪緵搴曞眰鏀寔涓庝竴涓畬鍏ㄥ彲鎺у埗鐨剆tream杩炴帴鎺ュ彛銆侸SO鐨勭洰鐨勬槸涓烘瀯寤哄熀浜嶫abber鐨勫鎴风/缁勪欢/鏈嶅姟鍣ㄦ彁渚涗竴涓彲瀹氬埗涓旈潪甯哥伒娲荤殑骞冲彴銆
绛旓細浣犲ソ锛氬悗鍙版媶鍒唈son private String interactPrizeAll; //json 浣跨敤瀛楃涓叉潵鎺ユ敹鏂规硶涓殑浠g爜锛欸son gson = new Gson();InteractPrize interactPrize =new InteractPrize(); //gson鐢ㄦ硾鍨 杞琇ist鏁扮粍 澶氫釜瀵硅薄List<InteractPrize > interactPrizeList = gson.fromJson(interactPrizeAll, new TypeToken<...
绛旓細JSONObject j = new JSONObject();j.put("id", "22");j.put("name", "haha");j.put("sex", "xixi");System.out.println(j.get("id"));
绛旓細java.util.List<User> list =userdao.findAll();java.util.List<User> 杩欎釜鏄痡dk 1.5浠ヤ笂鎵嶆湁鐨 鏄鏄庨泦鍚堢殑娉涚被 涔熷氨鏄噷闈㈢殑瀵硅薄 鏄粈涔堢被 for(User o:list) //杩欎篃鏄 jdk1.5 閲岄潰鐨 绫讳技 foreach锛坖s锛孋# 閲岄潰閮芥湁锛夊氨鏄 杩唬鍑簂ist 鐒跺悗杞崲涓 User 绫 鐢熸垚瀵硅薄 o 涓嶇煡閬撲綘...
绛旓細灏嗕竴涓猄tring 褰㈠紡鐨刯son杞寲鎴Map鐨Java绋嬪簭濡備笅:import java.util.HashMap;import java.util.Iterator;import java.util.Map;import org.json.JSONObject;public class FFF { public static void main(String[] args) { String s="{\"a\":\"1\",\"b\":2,\"c\":\"2016-5\"}"; ...
绛旓細棣栧厛妤间富鐨刯son鏁版嵁鏈夐棶棰樸備笅闈唬鐮佷翰娴嬶紝鍙敤锛屼袱涓枃浠舵斁鍦ㄥ悓绾х洰褰曪細Test.java public class Test { public static void main(String[] args) {ConvertXMLtoJSON();String jStr = "{\"users\": {\"user\": ["+ "{\"uid\": \"1\", \"node_id\": \"2\",\"children\": [...
绛旓細(1)鐢ㄦ枃鏈紪杈戝櫒鍐欎竴涓畝鍗曠殑java绋嬪簭锛歱ublic class HelloWorld { public static void main(String args[]) { System.out.println("Hello World!");} } 杩欎釜渚嬪瓙灏辨槸钁楀悕鐨勨淗ello World鈥濓紝瀹冪殑鍔熻兘灏辨槸鏄剧ず鈥淗ello World鈥濄傛敞鎰忥細璇ユ枃浠跺悕绉板繀椤讳负鈥淗elloWorld.java鈥濓紝澶у皬鍐欎篃鍖哄垎銆傜粏蹇冪殑鏈嬪弸...
绛旓細杩欎釜闂闂殑銆備綘浣跨敤javascript閫氳繃涓涓猦ttp鍘婚氱煡鏈嶅姟鍣紝鏈嶅姟鍣ㄩ噷鐨java浠g爜鍘绘彃鍏ユ暟鎹氨鍙互浜嗐俛jax 涓嶆槸鍏朵粬鎶鏈紝ajax涔熸槸绾痡s,鎵鏈夋祻瑙堝櫒閮芥敮鎸侊紝涓嶉渶瑕侀澶栨妧鏈傚鏋滀綘瀚岄夯鐑︼紝濡傛灉浣犵殑瑕佹眰涓嶉渶瑕佸叧蹇冧綘鎻掑叆鏄惁鎴愬姛杩樻槸澶辫触锛屽氨鏄湪娴忚鍣ㄧ涓嶆崟鑾锋槸鍚︽搷浣滄垚鍔燂紝绠鍗曚竴鐐广備娇鐢 鐏爣 鎶鏈
绛旓細4.鎵撳紑eclipse_js.epf鏂囦欢锛屾壘鍒皕java锛屽皢鍏跺鏀逛负锛.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW 鐒跺悗鎵惧埌zjs锛屽皢鍏跺兼敼涓猴細.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW 5.杩斿洖eclipse锛岄夋嫨宸︿笂瑙掕彍鍗旻ile->Import锛岄夋嫨General->Preferences锛屾壘鍒板垰鎵嶇殑淇濆瓨鐨勬枃浠讹紝閫夋嫨鏂囦欢瀹屾垚鍚庣偣鍑籉inish锛岃缃...