It is necessary to get from the json below "hello" and "foo";

{ "this": "succeeded", "by": "dweeting", "the": "dweet", "with": { "thing": "my-thing-name", "created": "2014-01-15T17:28:42.556Z", "content": { "hello": "1", "foo": "2" } } } 
  • Specify, "get" is to get the value, or make sure that there is such an element? - lospejos
  • Get value from hello and foo. - Viktoor 1:16 pm

2 answers 2

First of all: the original json string you quoted in the question is different from the one returned by the URL in your code (in any case, I differ). Further, judging by the print () in your answer, you use Python 3. If you focus on the line in question, then for Python 3:

 import json jsonstr = '''{ "this": "succeeded", "by": "dweeting", "the": "dweet", "with": { "thing": "my-thing-name", "created": "2014-01-15T17:28:42.556Z", "content": { "hello": "1", "foo": "2" } } }''' json = json.loads(jsonstr) helloval = json['with']['content']['hello'] fooval = json['with']['content']['foo'] print('hello={}'.format(helloval)) print('foo={}'.format(fooval)) 

    Happened

     import json import requests url = 'https://dweet.io/get/latest/dweet/for/my-thing-name' response = requests.get(url) resp = response.json() jstr = resp['with'] jstrl = jstr[0] content = jstrl['content'] print(content) print(content['foo']) print(content['hello']) 
    • The code does not match the example in question ( response['with']['content']['hello'] ). No nested list only dictionaries. - jfs