本站不再更新,欢迎光临 java开发技术网
随笔-230  评论-230  文章-8  trackbacks-0
package test;

import net.sf.json.*;
import java.util.*;
import domain.*;

public class JsonDemo {
    
public static void arraytojson(){
        String[] kkname
={"CCED","WPS","WORD"};
        JSONArray jr
=JSONArray.fromObject(kkname);
        System.out.println(jr);

    }
    
    
public static void list2json(){
        List
<Book> bklist=new ArrayList<Book>();
        bklist.add(
new Book("a1","b","111"));
        bklist.add(
new Book("a2","b","111"));
        bklist.add(
new Book("a3","精通JAVA","111"));
        JSONArray jr
=JSONArray.fromObject(bklist);
        System.out.println(jr);
    }
    
public static void createJson(){
        JSONArray jn
=JSONArray.fromObject("[{bookname:'JAVA程序设计',isbn:'885596-99',author:'小东'}]");//开头和结尾带[]
        List a=JSONArray.toList(jn, Book.class);
        Book book
=null;
        
for(Iterator it=a.iterator();it.hasNext();){
            book
=(Book)it.next();
            System.out.println(book.getAuthor()
+"-"+book.getBookname());
        }
    }
    
public static void Map2Json(){
        Map map
=new HashMap();
        map.put(
"name""小裴");
        map.put(
"book"new Book("K语言程序设计","","55588"));
        map.put(
"shoBookName","function(){alert('测试')}");
        JSONObject jo
=JSONObject.fromObject(map);
        System.out.println(jo);
    }
    
    
public static void bean2json(){
        JSONObject jo
=JSONObject.fromObject(new Book("1","3","ABCd"));
        System.out.println(jo);
    }
    
public static void json2bean(){
        JSONObject jo
=JSONObject.fromObject("{bookname:'C++程序设计',isbn:'885596-989',author:'小东'}");
        Book book
=(Book)JSONObject.toBean(jo,Book.class);
        System.out.println(book.getAuthor()
+"-"+book.getBookname());
        
    }
    
public static void main(String[] args)throws Exception{
        arraytojson();
        list2json();
        createJson();
        Map2Json();
        json2bean();
    }
}
posted on 2007-10-19 15:19 有猫相伴的日子 阅读(1646) 评论(1)  编辑  收藏 所属分类: XML/json相关技术

评论:
# re: json实例[未登录] 2013-12-09 21:50 | gg
~~~~  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 
本站不再更新,欢迎光临 java开发技术网