各種Json 和 Xml操作

2021-10-08 16:58:27 字數 2403 閱讀 8059

json注意

json返回到前台數字時 最好是字串 ,瀏覽器有可能將小數.00搞消失

//gson

1.create

gson gson = new gson();

gson gson = new gsonbuilder()

.setlenient()// json寬鬆 預設情況下,gson是嚴格的,只接受rfc 4627指定的json。此選項使解析器在接受的內容中更加自由。

.serializenulls() //解決null

.setdateformat("yyyy-mm-dd")//日期格式

//.setprettyprinting()格式化

//.enablecomplexmapkeyserialization()//支援map的key為複雜物件的形式

.create();

2.bean->jsonstr:

string gsonstr = gson.tojson(bean);

3.jsonstr->jsonobject:

略4.jsonstr->jsonarray:

5.jsonstr->bean:

int aaa = gson.fromjson(json, int.class);

listlist = gson.fromjson(listjsonstr,new typetoken>(){}.gettype());

6.從jsonobj中獲取jsonarray:

略7.從jsonarray中獲取某乙個json物件:

//fastjson

1.create://傳 true 使用的為linkedhashmap有序

new jsonobject(true);

2.bean->jsonstr://如果json值為null則在字串中key也看不見

string jsonstr = jsonobject.tojsonstring(data);

3.jsonstr->jsonobject://1可以對映為boolean的true,其他數字可以對映為boolean的false

jsonobject extjsonobj = jsonobject.parseobject(jsonstr);

4.jsonstr->jsonarray:

jsonarray array = jsonarray.parsearray(jsonstr);

5.jsonstr->bean:

object obj = jsonobject.parseobject(jsonstr, clazz);//jsonstr->bean

list> menus = json.parseobject(menujson, new typereference>>() {});//jsonstr->vo 要校驗jsonstr保證沒不報錯才可使用

6.從jsonobj中獲取jsonarray:

jsonarray array = jsonobject.getjsonarray("stationlist");

7.從jsonarray中獲取某乙個json物件:

jsonobject interval = intervalarray.getjsonobject(i);

8.策略

json.tojsonstring(result, serializerfeature.writemapnullvalue, serializerfeature.writenulllistasempty, serializerfeature.writenullstringasempty);

//jackson

1.create

//將物件 轉為xml格式字串  使用xstream

xstream xstream = new xstream();

xstream.alias("order", order.class);

xstream.alias("valuelist", code.class);

xstream.autodetectannotations(true);

// 去掉 class 屬性

xstream.aliassystemattribute(null, "class");

string xml = xstream.toxml(order);

xml和json格式輸出

class response type isset get format get format self json result array code code,message message,data data if type json elseif type array elseif type ...

php app介面實現(json和xml)

上篇學習了封裝mysql的例項化物件類!xml 擴充套件標記語言 可以標記資料 定義資料型別 資料格式清晰明了,可讀性高 json 一種輕量級的資料交換格式 生成資料簡單 傳輸速度快 獲取資料 從資料庫中或者快取中獲取資料 可以是快取裡的資料 提交資料 通過get方式或者post方式提交資料,服務端...

json和xml封裝介面詳解

下面我們開始封裝json介面資料 class response result array code code,message message,data data 輸出json資料 echo json encode result exit xml封裝 class response result arr...