微信小程序和JAVA的開發,屬於純天然的前后台分離開發,
微信小程序負責前端頁面的樣式,邏輯以及跳轉。
后台JAVA負責數據的封裝,業務邏輯,打包數據傳遞。(接口開發)
JSON是極為重要的工具:小程序之間傳遞JSON
1. JSON.stingify()可以將JSON對象或者數組轉換成json格式字符串
2. JSON.parse()將json格式的字符串,轉換成JSON對象或者數組
3. wx.request()微信小程序的請求發送中文數據時候,需要轉換
wx.request({
url: 'xxinterface',
header: {
"content-type": "application/x-www-form-urlencoded"
},
data: Util.json2Form({
data1: '中文數據'
}),
method: "POST",
success:function(e){
//成功回調
}
})
JAVA之間JSON數據使用:
1. list轉jsonarray:JSONArray ja = JSONArray.fromObject(list);
2. map轉jsonobject:JSONObject jo = JSONObject.fromObject(map);
3. javabean轉jsonobject:JSONObject jo = JSONObject.fromObject(new JavaBean());
4. string轉jsonobject:JSONObject jo = JSONObject.fromObject(str);
5. string轉jsonarray:JSONArray ja = JSONArray.fromObject(str);
使用:
JSONObject操作獲取key對應的value:
JSONObject jsonObject=JSONObject.fromObject(objectitem);
String productno = jsonObject.getString("productno");
Integer number = jsonObject.getInt("number");
JSONArray獲取第幾個JSONObject
jsonArray.get(0)
