变态重口极致另类在线-波多久久夜色精品国产-波多野结衣在线观看一区-波多野结衣在线观看一区二区-污污的网站免费阅读-污污视频网址

軟件首頁 |  文章首頁
最新更新 軟件分類 設為首頁 加入收藏 聯系我們
當前位置:首頁文章首頁 IT學院 Java

Java中Json格式數據的應用

作者:東坡下載  來源:uzzf  發布時間:2010-10-14 11:31:27  點擊:

      JSON是一種很簡潔很重要的數據格式,通常用來交換傳輸數據,廣泛使用于JavaScript技術中,并逐漸在各種流行編程語言中火了起來。在Java中也有一個JSON的庫,用來重要作用就是Java對象與JSON、XML數據的相互轉換,有著重要的應用。

  開源的JSON庫主頁:http://json-lib.sourceforge.net/

  環境:JDK5 , json-lib-2.3-jdk15

  所依賴的包:json-lib-2.3-jdk15.jar,commons-collections.jar,commons- lang.jar,commons-logging.jar,commons-beanutils.jar,ezmorph-1.0.6.jar,xom-1.1.jar

  java中各種類型所對應的json格式:

  1.數組或集合-->JSON串

  public static void test1() {

  System.out.println("------------數組或集合-->JSON串----------");

  boolean[] boolArray = new boolean[]{true, false, true};

  JSONArray jsonArray1 = JSONArray.fromObject(boolArray);

  System.out.println(jsonArray1);

  //輸出格式: [true,false,true]

  List list = new ArrayList();

  list.add("first");

  list.add("second");

  JSONArray jsonArray2 = JSONArray.fromObject(list);

  System.out.println(jsonArray2);

  //輸出格式: ["first","second"]

  JSONArray jsonArray3 = JSONArray.fromObject("['json','is','easy']");

  System.out.println(jsonArray3);

  //輸出格式: ["json","is","easy"]

  }

  2.Object|Map-->JSON串

  public static void test2() {

  System.out.println("------------Object|Map-->JSON串----------");

  Map map = new HashMap();

  map.put("name", "json");

  map.put("bool", Boolean.TRUE);

  map.put("int", new Integer(1));

  map.put("arr", new String[]{"a", "b"});

  map.put("func", "function(i){ return this.arr[i]; }");

  JSONObject jsonObject1 = JSONObject.fromObject(map);

  System.out.println(jsonObject1);

  //輸出格式: {"func":function(i){ return this.arr[i]; },"arr":["a","b"],"int":1,"bool":true,"name":"json"}

  JSONObject jsonObject2 = JSONObject.fromObject(new MyBean());

  System.out.println(jsonObject2);

  //輸出格式: {"func1":function(i){ return this.options[i]; },"func2":function(i){ return this.options[i]; },"name":"json","options":["a","f"],"pojoId":1}

  }

  public class MyBean {

  private String name = "json";

  private int pojoId = 1;

  private char[] options = new char[]{'a', 'f'};

  private String func1 = "function(i){ return this.options[i]; }";

  private JSONFunction func2 = new JSONFunction(new String[]{"i"}, "return this.options[i];");

  public String getName() {

  return name;

  }

  public void setName(String name) {

  this.name = name;

  }

  public int getPojoId() {

  return pojoId;

  }

  public void setPojoId(int pojoId) {

  this.pojoId = pojoId;

  }

  public char[] getOptions() {

  return options;

  }

  public void setOptions(char[] options) {

  this.options = options;

  }

  public String getFunc1() {

  return func1;

  }

  public void setFunc1(String func1) {

  this.func1 = func1;

  }

  public JSONFunction getFunc2() {

  return func2;

  }

  public void setFunc2(JSONFunction func2) {

  this.func2 = func2;

  }

  }

3.JSON串-->Object

  public static void test3() {

  System.out.println("------------JSON串-->Object----------");

  String json1 = "{name=\"json\",bool:true,int:1,double:2.2,func:function(a){ return a; },array:[1,2]}";

  JSONObject jsonObject1 = JSONObject.fromObject(json1);

  Object bean1 = JSONObject.toBean(jsonObject1);

  System.out.println(bean1);

  //net.sf.ezmorph.bean.MorphDynaBean@10dd1f7[

  //    {double=2.2, func=function(a){ return a; }, int=1, name=json, bool=true, array=[1, 2]}

  //]

  String json2 = "{bool:true,integer:1,string:\"json\"}";

  JSONObject jsonObject2 = JSONObject.fromObject(json2);

  BeanA bean2 = (BeanA) JSONObject.toBean(jsonObject2, BeanA.class);

  System.out.println(bean2);

  // BeanA{bool=true, integer=1, string='json'}

  }

  public class BeanA {

  private boolean bool;

  private Integer integer;

  private String string;

  public boolean isBool() {

  return bool;

  }

  public void setBool(boolean bool) {

  this.bool = bool;

  }

  public Integer getInteger() {

  return integer;

  }

  public void setInteger(Integer integer) {

  this.integer = integer;

  }

  public String getString() {

  return string;

文章評論

本類推薦文章

關于本站 | 網站幫助 | 廣告合作 | 下載聲明 | 友情連接 | 網站地圖
Copyright © 20098-2010 uzzf下載站. All Rights Reserved .
主站蜘蛛池模板: 波多野结衣在线观看免费区 | 日韩在线2020专区 | 久热色| 黄色网址免费观看视频 | 中文字幕视频在线 | 激情综合色五月丁香六月亚洲 | 波多野结衣免费免费视频一区 | 日本在线不卡免费 | 欧美日韩国产人成在线观看 | 亚洲国产中文字幕 | 成人亚洲国产 | 欧美一级特黄视频 | 美女黄色免费看 | 黄片毛片视频 | 亚洲欧美日韩中文无线码 | 91精品国产免费久久国语蜜臀 | 国产精品 视频一区 二区三区 | 一级片手机在线 | 韩国日本一区二区 | 免费在线观看成人 | 新神榜哪吒重生免费高清观看 | 国产精品免费小视频 | 免费污污视频在线观看 | 亚洲网站在线观看 | 日本不卡视频一区二区 | 天天插在线视频 | 91亚洲免费视频 | 免费在线观看黄网站 | 亚久久伊人精品青青草原2020 | 国产欧美日韩不卡在线播放在线 | 精品国产欧美一区二区 | 日本乱人伦片中文三区 | 在线视频亚洲欧美 | 福利社午夜影院 | 亚洲精品第四页中文字幕 | 亚洲综合专区 | 精品毛片视频 | 一区二区中文字幕在线观看 | 一级黄色大片免费观看 | 岛国毛片在线 | 中文字幕巨乱亚洲 |