JSON字符串转换JAVA对象例子。
2013-05-17 22:59
351 查看
JsonTest:
JsonTool:
People:
Content:
pictures:
控制台运行结果:
package com.demo.json; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.json.simple.JSONValue; public class JsonTest { public static void main(String[] args) { // ----------------------------------------------------------------------------- // Object 2 JSON List<Peoper> peopers = new ArrayList<Peoper>(); Peoper p1 = new Peoper("001", "Taki", "中国"); Peoper p2 = new Peoper("002", "DSM", "China"); peopers.add(p1); peopers.add(p2); String result = JsonTool.getJsonString("Peopers", peopers); System.out.println("JSON: " + result); // 解析PHP json_encode 字符串 String jsonStr = "{\"Name\":\"\u5e0c\u4e9a\",\"Age\":20}"; Object obj = JSONValue.parse(jsonStr); System.out.println(obj); System.out.println(); // ----------------------------------------------------------------------------- // JSON 2 Object String jsonString = "[" + "{\"author\":\"7\",\"id\":358,\"title\":\"Japan\",\"pictures\":[{\"description\":\"001\",\"imgPath\":\"/cms/u/cms/www/201203/05150720ii68.jpg\"},{\"description\":\"002\",\"imgPath\":\"/cms/u/cms/www/201203/05150720ii67.jpg\"}],\"path\":\"ip\"}," + "{\"author\":\"8\",\"id\":359,\"title\":\"China\",\"pictures\":[{\"description\":\"101\",\"imgPath\":\"/cms/u/cms/www/201203/111111111111.jpg\"},{\"description\":\"102\",\"imgPath\":\"/cms/u/cms/www/201203/222222222222.jpg\"}],\"path\":\"ip\"}]"; JSONArray array = JSONArray.fromObject(jsonString); // Content.class包含pictures.class,需要设置这个参数 Map<String, Class<pictures>> classMap = new HashMap<String, Class<pictures>>(); classMap.put("pictures", pictures.class); Object[] objs = new Object[array.size()]; for (int i = 0; i < array.size(); i++) { JSONObject jsonObject = array.getJSONObject(i); objs[i] = (Content) JSONObject.toBean(jsonObject, Content.class, classMap); } // 转换Content,循环输出所有content for (int i = 0; i < objs.length; i++) { Content content = (Content) objs[i]; System.out.println("author:" + content.getAuthor() + " ID:" + content.getId() + " Title:" + content.getTitle() + " Path:" + content.getPath()); // 转换pictures,循环输出所有picture List<pictures> pictures = content.getPictures(); for (int n = 0; n < pictures.size(); n++) System.out.println("description:" + pictures.get(n).getDescription() + " imgPath:" + pictures.get(n).getImgPath()); } } }
JsonTool:
package com.demo.json; import java.util.regex.Matcher; import java.util.regex.Pattern; import net.sf.json.JSONObject; public class JsonTool { public static String getJsonString(Object key, Object value) { //System.out.println("key: " + key); //System.out.println("value: " + value.toString()); JSONObject obj = new JSONObject(); obj.put(key, value); //添加物件 return obj.toString(); //转换为字符串并返回 } //解析PHP json_encode 字符串 public static String unescapeUnicode(String str) { StringBuffer b=new StringBuffer(); Matcher m = Pattern.compile("\\\\u([0-9a-fA-F]{4})").matcher(str); while(m.find()) { b.append((char)Integer.parseInt(m.group(1),16)); } return b.toString(); } }
People:
package com.demo.json; public class People { public People() { } public People(String id, String name, String address) { this.id = id; this.name = name; this.address = address; } private String id; private String name; private String address; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String toString() { return "ID:" + this.id + " Name:" + this.name + " Address:" + this.address; } }
Content:
package com.demo.json; import java.util.List; public class Content { private String author; private String id; private String title; private List<pictures> pictures; private String path; public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public List<pictures> getPictures() { return pictures; } public void setPictures(List<pictures> pictures) { this.pictures = pictures; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } }
pictures:
package com.demo.json; public class pictures { private String description; private String imgPath; public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getImgPath() { return imgPath; } public void setImgPath(String imgPath) { this.imgPath = imgPath; } }
控制台运行结果:
JSON: {"Peopers":[{"address":"中国","id":"001","name":"Taki"},{"address":"Japan","id":"002","name":"DSM"}]} {"Name":"中文内容","Age":20} author:7 ID:358 Title:Japan Path:ip description:001 imgPath:/cms/u/cms/www/201203/05150720ii68.jpg description:002 imgPath:/cms/u/cms/www/201203/05150720ii67.jpg author:8 ID:359 Title:China Path:ip description:101 imgPath:/cms/u/cms/www/201203/111111111111.jpg description:102 imgPath:/cms/u/cms/www/201203/222222222222.jpg
相关文章推荐
- JSON字符串转换JAVA对象例子。
- FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换操作
- spring mvc传入参数不仅仅转换为json,还可以直接将json字符串转换为具体的java对象
- Json对象与Json字符串的转化、JSON字符串与Java对象的转换
- Json对象、Json字符串与Java对象转换
- JSON 字符串 与 java 对象的转换
- Jsonlib实现Json字符串和Java对象的互相转换
- Gson在java对象和json字符串之间转换
- Java之使用json-lib进行对象与json字符串之间的转换
- JSON 字符串 与 java 对象的转换
- Java中Json字符串直接转换为对象(包括多层List集合 嵌套)
- 用Gson包进行java对象和Json格式字符串的相互转换
- Json对象与Json字符串的转化、JSON字符串与Java对象的转换
- 非标准json,标准json,json字符串与js对象,Java对象之间的相互转换
- JSON 字符串 与 java 对象的转换
- FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换
- JSON 字符串 与 java 对象的转换
- json对象、json格式字符串、java对象【使用此net.sf.json.JSONObject将java对象[比如数组集合Map、Map扩展]转换成json格式字符串】
- 关于json以及json字符串和Java对象的转换
- java中把对象、对象bean、list集合、对象数组、Map和Set以及字符串转换成Json