在使用python转储创建请求时操作双引号

时间:2015-12-02 20:52:20

标签: python json

寻找一些自定义json样式语法请求的建议。

json.dumps将格式化为:

{"tenantid": 1, "name": "NewRoleName", "users": "[djones]"}

但服务器需要这样的东西:

{"tenantid": 1, "name": "NewRoleName", "users": ["djones"]}

操纵双引号的简单方法是放在方括号中吗?

payload = {}
funcURL = "roles"
if name          is not None: payload.update({'name': name})
if userStore     is not None: payload.update({'userStore': userStore})
if users         is not None: payload.update({'users': "[" + users + "]"})
if tenantid      is not None: payload.update({'tenantid': tenantid})
if permissions   is not None: payload.update({'userClaims': permissions})
print users
print payload
payload = json.dumps(payload)
return self._call_url('POST', funcURL, payload)

_call_url使用请求发送有效负载:

response = requests.request( methodType, baseURL + "/" + funcURL, data=payload, headers=self.headers, timeout=timeout, verify=False)

2 个答案:

答案 0 :(得分:0)

你试过了吗?

spinnerList.add(Html.fromHtml("NO<sub>2</sub> - maximum 1-hour concentration"));

如果Sub PrintArea() Dim I As Integer Dim ws As Worksheet WS_Count = ActiveWorkbook.Worksheets.Count For I = 1 To 6 Set ws = ThisWorkbook.Sheets(I) ws.PageSetup.PrintArea = ws.Range("A1:Z24").Address With ws.PageSetup .Orientation = xlLandscape .FitToPagesWide = 1 .FitToPagesTall = 1 End With Next I End Sub 类似于if users is not None: payload.update({'users': users})

,这应该有用

答案 1 :(得分:0)

方括号表示一个列表。所以你必须把users放到一个列表中:

funcURL = "roles"
payload = {}
if name is not None:
    payload['name'] = name
if userStore is not None:
    payload['userStore'] = userStore
if users is not None:
    payload['users'] = [users]
if tenantid is not None:
    payload['tenantid'] = tenantid
if permissions is not None:
    payload['userClaims'] = permissions
payload = json.dumps(payload)
return self._call_url('POST', funcURL, payload)