東坡下載:內(nèi)容最豐富最安全的下載站!

首頁(yè)編程開發(fā)Java → Java中Json格式數(shù)據(jù)的應(yīng)用

Java中Json格式數(shù)據(jù)的應(yīng)用

相關(guān)文章發(fā)表評(píng)論 來(lái)源:本站時(shí)間:2010/10/14 11:31:27字體大小:A-A+

更多

作者:東坡下載點(diǎn)擊:1932次評(píng)論:0次標(biāo)簽:

      JSON是一種很簡(jiǎn)潔很重要的數(shù)據(jù)格式,通常用來(lái)交換傳輸數(shù)據(jù),廣泛使用于JavaScript技術(shù)中,并逐漸在各種流行編程語(yǔ)言中火了起來(lái)。在Java中也有一個(gè)JSON的庫(kù),用來(lái)重要作用就是Java對(duì)象與JSON、XML數(shù)據(jù)的相互轉(zhuǎn)換,有著重要的應(yīng)用。

  開源的JSON庫(kù)主頁(yè):http://json-lib.sourceforge.net/

  環(huán)境: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中各種類型所對(duì)應(yīng)的json格式:

  1.數(shù)組或集合-->JSON串

  public static void test1() {

  System.out.println("------------數(shù)組或集合-->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;

  }

  public void setString(String string) {

  this.string = string;

  }

  @Override

  public String toString() {

  return "BeanA{"bool=" + bool +", integer=" + integer +", string='" + string + '\'' +"}";

  }

  }

  4.JSON串-->XML

  public static void test4() {

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

  JSONObject json = new JSONObject(true);

  String xml = new XMLSerializer().write(json);

  System.out.println(xml);

  JSONObject json1 = JSONObject.fromObject("{\"name\":\"json\",\"bool\":true,\"int\":1}");

  String xml1 = new XMLSerializer().write(json1);

  System.out.println(xml1);

  JSONArray json2 = JSONArray.fromObject("[1,2,3]");

  String xml2 = new XMLSerializer().write(json2);

  System.out.println(xml2);

  }

  5.XML-->JSON串

  public static void test5() {

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

  String xml = "" +

  "<a class=\"array\">\n" +

  "    <e type=\"function\" params=\"i,j\">\n" +

  "            return matrix[i][j];\n" +

  "    </e>\n" +

  "</a>";

  JSONArray json = (JSONArray) new XMLSerializer().read(xml);

  System.out.println(json);

  }

  特別注意:

  1、所有的Bean都應(yīng)該定義為public,否則會(huì)出現(xiàn)net.sf.json.JSONException: java.lang.NoSuchMethodException: Property '***' has no getter method的錯(cuò)誤。

  2、必須引入xom-1.1.jar包,否則拋出java.lang.NoClassDefFoundError: nu/xom/Serializer

相關(guān)評(píng)論

閱讀本文后您有什么感想? 已有 人給出評(píng)價(jià)!

  • 2791 喜歡喜歡
  • 2101 頂
  • 800 難過(guò)難過(guò)
  • 1219 囧
  • 4049 圍觀圍觀
  • 5602 無(wú)聊無(wú)聊
熱門評(píng)論
最新評(píng)論
發(fā)表評(píng)論 查看所有評(píng)論(0)
昵稱:
表情: 高興 可 汗 我不要 害羞 好 下下下 送花 屎 親親
字?jǐn)?shù): 0/500 (您的評(píng)論需要經(jīng)過(guò)審核才能顯示)