「JSON概述」(http://www.xiaoboswift.com/course/91/task/816/show)
备注:
SwiftyJSON-Swift中的Json解析
1. JSON编码:对象转为Json文本
JSON编码:一种编程语言的对象转成JavaScript的文本表示
(1)Json文本示例1
{
"resultcode": "200",
"reason": "查询成功!",
"result": {
"sk": { /*当前实况天气*/
"temp": "21", /*当前温度*/
"wind_direction": "西风", /*当前风向*/
"wind_strength": "2级", /*当前风力*/
"humidity": "4%", /*当前湿度*/
"time": "14:25" /*更新时间*/
},
"today": {
"city": "天津",
"date_y": "2014年03月21日",
"week": "星期五",
"temperature": "8℃~20℃", /*今日温度*/
"weather": "晴转霾", /*今日天气*/
"weather_id": { /*天气唯一标识*/
"fa": "00", /*天气标识00:晴*/
"fb": "53" /*天气标识53:霾 如果fa不等于fb,说明是组合天气*/
},
"wind": "西南风微风",
"dressing_index": "较冷", /*穿衣指数*/
"dressing_advice": "建议着大衣、呢外套加毛衣、卫衣等服装。", /*穿衣建议*/
"uv_index": "中等", /*紫外线强度*/
"comfort_index": "",/*舒适度指数*/
"wash_index": "较适宜", /*洗车指数*/
"travel_index": "适宜", /*旅游指数*/
"exercise_index": "较适宜", /*晨练指数*/
"drying_index": ""/*干燥指数*/
},
"future": [ /*未来几天天气*/
{
"temperature": "28℃~36℃",
"weather": "晴转多云",
"weather_id": {
"fa": "00",
"fb": "01"
},
"wind": "南风3-4级",
"week": "星期一",
"date": "20140804"
},
{
"temperature": "28℃~36℃",
"weather": "晴转多云",
"weather_id": {
"fa": "00",
"fb": "01"
},
"wind": "东南风3-4级",
"week": "星期二",
"date": "20140805"
},
{
"temperature": "27℃~35℃",
"weather": "晴转多云",
"weather_id": {
"fa": "00",
"fb": "01"
},
"wind": "东南风3-4级",
"week": "星期三",
"date": "20140806"
},
{
"temperature": "27℃~34℃",
"weather": "多云",
"weather_id": {
"fa": "01",
"fb": "01"
},
"wind": "东南风3-4级",
"week": "星期四",
"date": "20140807"
},
{
"temperature": "27℃~33℃",
"weather": "多云",
"weather_id": {
"fa": "01",
"fb": "01"
},
"wind": "东北风4-5级",
"week": "星期五",
"date": "20140808"
},
{
"temperature": "26℃~33℃",
"weather": "多云",
"weather_id": {
"fa": "01",
"fb": "01"
},
"wind": "北风4-5级",
"week": "星期六",
"date": "20140809"
},
{
"temperature": "26℃~33℃",
"weather": "多云",
"weather_id": {
"fa": "01",
"fb": "01"
},
"wind": "北风4-5级",
"week": "星期日",
"date": "20140810"
}
]
},
"error_code": 0
}
(2)Json文本示例2
{
"code": "0",
"msg": "success",
"res": {
"machineList": [
{
"cm_id": "319",
"company_id": "50",
"machine_id": "32",
"cate": {
"cate_id": "102",
"sort": "0",
"parent_id": "2",
"cate_name": "MA04自动连续锁眼机",
"image": "/upload/image/2018/09/60d9f57d939c310894db9f819128e36c.jpg",
"machine_num": "0",
"unit": "个",
"time_unit": "秒",
"is_jtj": "0",
"tiered_num": "0",
"tiered_discount": "0.00"
},
"cust_group": "10",
"is_lock": "0",
"is_rent": "0",
"status": "1",
"machine": {
"machine_id": "32",
"cate": "2",
"cate2": "102",
"name": "问题测试 ",
"sn": "Qtest",
"channel": "1",
"price": "0.000",
"description": null,
"image": "http://s.uchat.com.cn/upload/image/2018/09/60d9f57d939c310894db9f819128e36c_150x150.jpg",
"tags": null,
"protocol": null,
"location": "",
"device_id": "39590889",
"imsi": null,
"ProductKey": null,
"DeviceName": null,
"DeviceSecret": null,
"IotId": null,
"lac": "22706",
"ci": "48888",
"csq": "0",
"lat": "",
"lng": "",
"software": null,
"build_time": "0",
"hardware": null,
"ota_time": "0",
"add_time": "1534904725",
"rent_out": "1",
"status": "0",
"status_time": "1535339005",
"login_type": "7",
"maintain_time": "0",
"last_time": "0",
"params": null,
"param_time": "0",
"parameter": null,
"params_set": "{\"\":null}",
"devctrl": "1",
"sale_type": "2",
"is_del": "0"
},
"use_time": 0,
"i": 1
},
{
"cm_id": "318",
"company_id": "50",
"machine_id": "33",
"cate": "0",
"cust_group": "10",
"is_lock": "0",
"is_rent": "0",
"status": "1",
"machine": {
"machine_id": "33",
"cate": "2",
"cate2": "0",
"name": "迈卡袖衩机",
"sn": "0109",
"channel": "1",
"price": "0.300",
"description": null,
"image": "http://s.uchat.com.cn/public/images/nopic300.png",
"tags": null,
"protocol": null,
"location": "",
"device_id": "39597199",
"imsi": null,
"ProductKey": null,
"DeviceName": null,
"DeviceSecret": null,
"IotId": null,
"lac": "0",
"ci": "0",
"csq": "0",
"lat": "",
"lng": "",
"software": null,
"build_time": "0",
"hardware": null,
"ota_time": "0",
"add_time": "1534917235",
"rent_out": "1",
"status": "0",
"status_time": null,
"login_type": "7",
"maintain_time": "0",
"last_time": "0",
"params": null,
"param_time": "0",
"parameter": null,
"params_set": "{\"\":null}",
"devctrl": "1",
"sale_type": "2",
"is_del": "0"
},
"use_time": 0,
"i": 2
},
{
"cm_id": "306",
"company_id": "50",
"machine_id": "25",
"cate": "0",
"cust_group": "0",
"is_lock": "0",
"is_rent": "1",
"status": "1",
"machine": {
"machine_id": "25",
"cate": "2",
"cate2": "0",
"name": "开发测试(agent)",
"sn": "LHCS02",
"channel": "1",
"price": "0.000",
"description": null,
"image": "http://s.uchat.com.cn/public/images/nopic300.png",
"tags": null,
"protocol": null,
"location": "",
"device_id": "38785262",
"imsi": "460040148224239",
"ProductKey": null,
"DeviceName": null,
"DeviceSecret": null,
"IotId": null,
"lac": "22706",
"ci": "48888",
"csq": "12",
"lat": "",
"lng": "",
"software": "macia_datacollector_v2.0.0",
"build_time": "0",
"hardware": "Protocol_Converter_v3.5",
"ota_time": "0",
"add_time": "1533892588",
"rent_out": "1",
"status": "0",
"status_time": "1545917649",
"login_type": "7",
"maintain_time": "0",
"last_time": "0",
"params": null,
"param_time": "0",
"parameter": null,
"params_set": "{\"\":null}",
"devctrl": "1",
"sale_type": "1",
"is_del": "0"
},
"use_time": 0,
"i": 3
},
{
"cm_id": "189",
"company_id": "50",
"machine_id": "142",
"cate": {
"cate_id": "106",
"sort": "0",
"parent_id": "2",
"cate_name": "MCD门襟卷缝机",
"image": "/upload/image/2018/09/09e8b04e355df957610c6e33d81275de.jpg",
"machine_num": "0",
"unit": "个",
"time_unit": "秒",
"is_jtj": "0",
"tiered_num": "0",
"tiered_discount": "0.00"
},
"cust_group": "0",
"is_lock": "0",
"is_rent": "0",
"status": "1",
"machine": {
"machine_id": "142",
"cate": "2",
"cate2": "106",
"name": "门襟卷缝机(一楼测试2)",
"sn": "MCD201793",
"channel": "1",
"price": "0.000",
"description": null,
"image": "http://s.uchat.com.cn/upload/image/2018/09/09e8b04e355df957610c6e33d81275de_150x150.jpg",
"tags": null,
"protocol": null,
"location": "",
"device_id": "505776789",
"imsi": "460040148225988",
"ProductKey": null,
"DeviceName": null,
"DeviceSecret": null,
"IotId": null,
"lac": "26475",
"ci": "12266",
"csq": "0",
"lat": "",
"lng": "",
"software": "macia_datacollector_v2.0.0",
"build_time": "0",
"hardware": "Protocol_Converter_v3.5",
"ota_time": "0",
"add_time": "1544418000",
"rent_out": "1",
"status": "0",
"status_time": "1546847997",
"login_type": "7",
"maintain_time": "1000",
"last_time": "0",
"params": null,
"param_time": "0",
"parameter": null,
"params_set": null,
"devctrl": "0",
"sale_type": "2",
"is_del": "0"
},
"use_time": 0,
"i": 4
}
],
"machineNum": 4,
"machineGroup": [
{
"group_id": "10",
"name": "包缝机"
}
],
"onlineNum": 0,
"company_id": 50
}
}
猜你喜欢
-
上万种菜色供您选择
-
查询历史天气,目前支持2000+城市、县、地区历史天气信息,主要指标包括每天最高气温、最低气温、天气状况、风向等
-
支持大多数快递公司
-
稳定的短信发送API,低至3.7分/条;【请务必添加图片验证码等防恶意攻击的机制,以防短信轰炸】。
-
城市空气质量、城市空气PM2.5指数、城市辐射指数
2. JSON解码:Json文本转为对象
(从服务器获取的json文本转为当前开发语言所用的对象)
3. JSON编解码
3.1 数据模型:M(该对象要编解码则需遵从Codable协议)
struct Device:Codable {
var deviceId:String
var deviceName:String
}
3.2 控制器:C
(1)自定义数组对象
var devices = [Device(deviceId: "001", deviceName: "平缝机"),Device(deviceId: "002", deviceName: "包缝机"),Device(deviceId: "003", deviceName: "绷缝机"),Device(deviceId: "004", deviceName: "特种机"),Device(deviceId: "005", deviceName: "裁床"),Device(deviceId: "005", deviceName: "绣花机")]
(2)Json编码
//Json编码:对象转json文本
func saveToJson() {
let coder = JSONEncoder()
do {
let data = try coder.encode(devices)
let saveUrl = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("devices.json")
try data.write(to: saveUrl)
print("保存成功!路径:",saveUrl)
} catch {
print("编码错误:",error)
}
}
(3) 获取json文件(devices.json)并复制到项目目录下(主包)
[{"deviceName":"平缝机","deviceId":"001"},{"deviceName":"包缝机","deviceId":"002"},{"deviceName":"绷缝机","deviceId":"003"},{"deviceName":"特种机","deviceId":"004"},{"deviceName":"裁床","deviceId":"005"},{"deviceName":"绣花机","deviceId":"005"}]
格式化Json文件后
[{
"deviceName": "平缝机",
"deviceId": "001"
}, {
"deviceName": "包缝机",
"deviceId": "002"
}, {
"deviceName": "绷缝机",
"deviceId": "003"
}, {
"deviceName": "特种机",
"deviceId": "004"
}, {
"deviceName": "裁床",
"deviceId": "005"
}, {
"deviceName": "绣花机",
"deviceId": "005"
}]
(4)Json解码:将主包中的json文件(devices.json)转为数组对象
//Json解码:json文本转对象
func loadJson() {
let coder = JSONDecoder()
do {
//从本地主包中找到devices.json文件
let url = Bundle.main.url(forResource: "devices", withExtension: "json")!
//读取本地json数据(从地址中取到数据)
let data = try Data(contentsOf: url)
//将json数据解码为Device数据(对象)
devices = try coder.decode([Device].self, from: data)
print("解码成功:",devices[0].deviceId)
} catch {
print("解码错误:",error)
}
}
4. JSON编辑器
(1)Visual Studio Code
(2)NotePad++
(3)jsoneditoronline
网友评论