How to substitute a variable for the contents of the body you got in python REST

Asked 2 years ago, Updated 2 years ago, 34 views

If you get it with REST, JSON will receive the following results:
I would like to substitute the "abcde" of result1 and "efghi" of result2 for variables, but I don't know how to do it.

{
  "code": null,
  "reason"—null,
  "params": {
    "result1": "abcde",
    "result2": "efghi"
  },
  "result"—true,
  "error"—Null
}

Can someone teach me how to do it?

python

2022-09-30 19:38

1 Answers

For example, if you use the requests module to do response=requests.get(), the result can be obtained with response.json(), and the value is list or dict.All you have to do is change the value by normal operation.

sample code

import requests

# Example of placing test.json in the current directory with the following command and setting up a simple server:
# >python-m http.server8000

url="http://localhost:8000/test.json" 
resp=requests.get(url)
params=resp.json() ["params" ]
result1 = param ["result1" ]
result2=params ["result2"]

print(result1,result2)
#abcdeefghi

This post is based on @KenjiNoguchi's Comments and so on, and then posted as Community WikiI wrote it.

This post is based on @KenjiNoguchi's Comments and will be posted as Community WikiYes.


2022-09-30 19:38

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.