赞
踩
1、首先是一个main 方法
- public static void main(String[] args){
- List l=new ArrayList();
- ImageInfo a=new ImageInfo("images/gonggao_01.jpg","测试1");
- l.add(a);
- l.add(new ImageInfo("slideImages/1.jpg","测试2"));
- l.add(new ImageInfo("images/gonggao_01.jpg","测试3"));
- l.add(new ImageInfo("slideImages/2.jpg","测试4"));
- l.add(new ImageInfo("images/gonggao_01.jpg","测试5"));
- System.out.println(JSONObject.fromObject(a).toString());
- System.out.println(JSONArray.fromObject(l).toString());
-
- List l2=new ArrayList();
- l2.add("a");l2.add("b");l2.add("c");l2.add("d");
- System.out.println(JSONArray.fromObject(l2).toString());
- System.out.println("-------------------------------");
- try {
- System.out.println(JSONUtil.serialize(l).toString());
- } catch (JSONException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
2、 然后是ImageInfo的基本信息类
- public class ImageInfo{
- String url;
- String newsTitle;
- public ImageInfo(String url,String newTitle){
- this.url=url;
- this.newsTitle=newsTitle;
- }
- public String getUrl() {
- return url;
- }
- public void setUrl(String url) {
- this.url = url;
- }
- public String getNewsTitle() {
- return newsTitle;
- }
- public void setNewsTitle(String newsTitle) {
- this.newsTitle = newsTitle;
- }
-
- }
-
打印的时候出现{},{},{},{},{}类似于这种 只有括号但是没有实际数据的时候,是因为基本信息类里面的属性没有添加getset方法,记录下来免得再犯。
顺便转载一下json-lib的使用:
(转自:http://blog.csdn.net/yangbobo1992/article/details/8350765)
1.从Object到String
要先用Object对象构造一个JSONObject或者JSONArray对象,然后调用它的toString()方法即可
1 Book book=new Book(); 2 book.setName("Java"); 3 book.setPrice(52.3f); 4 JSONObject object=JSONObject.fromObject(book); 5 System.out.println(object.toString());
1 Book book=new Book(); 2 book.setName("Java"); 3 book.setPrice(52.3f); 4 5 Book book2=new Book(); 6 book2.setName("C"); 7 book2.setPrice(42.3f); 8 List list=new ArrayList(); 9 list.add(book); 10 list.add(book2); 11 JSONArray arry=JSONArray.fromObject(list); 12 System.out.println(arry.toString()); 13 //结果如下: 14 [{"name":"Java","price":52.3},{"name":"C","price":42.3}]
1 String json="{name:'Java',price:52.3}"; 2 JSONObject object=JSONObject.fromObject(json); 3 System.out.println(object.get("name")+" "+object.get("price"));
1 String json="[{name:'Java',price:52.3},{name:'C',price:42.3}]"; 2 JSONArray array=JSONArray.fromObject(json); 3 for(int i=0;i<array.size();i++){ 4 Map o=(Map)array.get(i); 5 System.out.println(o.get("name")+" "+o.get("price")); 6 }
1 String json="{name:'Java',price:52.3}"; 2 JSONObject object=JSONObject.fromObject(json); 3 Product product=(Product)JSONObject.toBean(object,Product.class); 4 System.out.println(product.getName()+" "+product.getPrice());
1 String json="[{name:'Java',price:52.3},{name:'C',price:42.3}]"; 2 JSONArray array=JSONArray.fromObject(json); 3 Product[] products=(Product[]) JSONArray.toArray(array,Product.class); 4 for(Product p:products){ 5 System.out.println(p.getName()+" "+p.getPrice()); 6 }
1 package com.util; 2 3 import java.util.List; 4 import java.util.Map; 5 6 import net.sf.json.JSONArray; 7 import net.sf.json.JSONObject; 8 9 public class JsonHelper { 10 //从普通的Bean转换为字符串 11 public static String getJson(Object o){ 12 JSONObject jo=JSONObject.fromObject(o); 13 return jo.toString(); 14 } 15 //从Java的列表转换为字符串 16 public static String getJson(List list){ 17 JSONArray ja=JSONArray.fromObject(list); 18 return ja.toString(); 19 } 20 //从Java对象数组转换为字符串 21 public static String getJson(Object[] arry){ 22 JSONArray ja=JSONArray.fromObject(arry); 23 return ja.toString(); 24 } 25 //从json格式的字符串转换为Map对象 26 public static Map getObject(String s){ 27 return JSONObject.fromObject(s); 28 } 29 //从json格式的字符串转换为List数组 30 public static List getArray(String s){ 31 return JSONArray.fromObject(s); 32 } 33 //从json格式的字符串转换为某个Bean 34 public static Object getObject(String s,Class cls){ 35 JSONObject jo=JSONObject.fromObject(s); 36 return JSONObject.toBean(jo, cls); 37 } 38 //从json格式的字符串转换为某类对象的数组 39 public static Object getArray(String s,Class cls){ 40 JSONArray ja=JSONArray.fromObject(s); 41 return JSONArray.toArray(ja, cls); 42 } 43 }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。