日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

(转)使用json-lib 数据格式转换

發(fā)布時間:2025/3/17 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (转)使用json-lib 数据格式转换 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

下載

??? 到http://json-lib.sourceforge.net下載json-lib-1.1-jdk15.jar,運行json還需要的包有:commons-lang.jar、commons-logging.jar,commons-beanutils.jar、xom-1.0-2005-01-05.jar、ezmorph-1.0.1.jar,可在http://www.docjar.com搜索下載。

???

l???????? 轉(zhuǎn)化數(shù)組和集合

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

?????? JSONArray jsonArray = JSONArray.fromObject(boolArray);

?????? System.out.println(jsonArray);

??? 輸出:[true,false,true]

??? List list = new ArrayList();

?????? list.add(“第一個”);

?????? list.add(“第二個”);

?????? JSONArray jsonArray = JSONArray.fromObject(list);

??? System.out.println(jsonArray);

??? 輸出:[“第一個”, “第二個”]

??????

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

??? System.out.println(jsonArray3);

??? 輸出:[“json”, “is”, “'easy'”]

l???????? 轉(zhuǎn)化對象

轉(zhuǎn)化Map

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 json = JSONObject.fromObject(map);

System.out.println(json);

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

轉(zhuǎn)化Bean

MyBean.java

public class MyBean {

??? private String name = "json";

??? private int pojoId = 1;

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

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

???

??? //以下為get、set方法

}

MyBean bean = new MyBean();

JSONObject jsonObject = JSONObject.fromObject(bean);

System.out.println(jsonObject);

輸出:

{"func1":function(i){return this.options[i]},"pojoId":1,"name":"json","func2":function(i){ return this.options[i]; }

從JSON到Beans

//轉(zhuǎn)化為動態(tài)bean

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

JSONObject json1 = JSONObject.fromString(myjson);

Object bean1 = JSONObject.toBean(json1);

由JSON生成XML

JSONObject json = new JSONObject(true);

XMLSerializer xmlSerializer = new XMLSerializer();

String xml = xmlSerializer.write(json);

System.out.println("xml:" + xml);

輸出:xml:<?xml version="1.0" encoding="UTF-8"?>

<o null="true"/>

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

String xml2 = xmlSerializer.write(json2);

System.out.println("xml2:" + xml2);

輸出:xml2:<?xml version="1.0" encoding="UTF-8"?>

<o><bool type="boolean">true</bool><int type="number">1</int><name type="string">json</name></o>

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

String xml3 = xmlSerializer.write(json3);

System.out.println("xml3:" + xml3);

輸出:xml3:<?xml version="1.0" encoding="UTF-8"?>

<a><e type="number">1</e><e type="number">2</e><e type="number">3</e></a>

本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/xueyepiaoling/archive/2011/02/24/6204374.aspx

轉(zhuǎn)載于:https://www.cnblogs.com/joly-han/archive/2011/06/17/2083742.html

總結(jié)

以上是生活随笔為你收集整理的(转)使用json-lib 数据格式转换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。