在调用接口时,可能接口要求在参数中传递"\",示例代码如下:

import json

mcuid = 'safsdfass'
oper_type = 1
pos_id = 1

body = {"appId":131312324,"endpointOS":1,"clientVersion":"","deviceId":"fasdfasdfa","mcuid":"111","datatype":1,"datapoint":"{\"index\":1,\"status\":0}","context":"sssssss"}
body["mcuid"] = mcuid

# if 1 == oper_type :
if oper_type in [1,3] :
    if 1 == oper_type :
        body["datapoint"] = "{\\\"index\\\":1,\\\"status\\\":0}"
    else:
        body["datapoint"] = "{\\\"index\\\":" + str(int(pos_id) + 1) + ",\\\"status\\\":0}"
# elif 2 == oper_type :
elif oper_type in [2,4] :
    if 2 == oper_type:
        body["datapoint"] = "{\\\"index\\\":1,\\\"status\\\":1}"
    else:
        body["datapoint"] = "{\\\"index\\\":" + str(int(pos_id)+1) + ",\\\"status\":1}"

re = json.dumps(body)
print(re)
bb = re.replace('\\\\', '\\').replace('\\\\', '\\')
print(bb)

执行截图如下:

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐