0

How to "get" JSON string?
Call API Server code:

import requests
file_name = os.path.join('','/home/pp2018/public/users/roger123/000000000285.jpg')
files = {'file': open(file_name, 'rb')}
url = "http://111.111.111.111:57149"
res = requests.post(url,files=files)
print(res.json())

Return value:

{"pic1": {"class_name": "dog1", "Pos": [11.11, 22.22, 33.33, 44.44], "Confidence": 0.98},
 "pic2": {"class_name": "dog2", "Pos": [99.99, 88.88, 77.77, 66.66], "Confidence": 0.99},
 "pic3": {"class_name": "dog3", "Pos": [33.33, 44.44, 55.55, 66.66], "Confidence": 0.97},
}  

object command like

 GET --> print(***.pic2.Pos[0])            >>>  99.99   

my question is how to get 99.99

Willie Cheng
  • 7,679
  • 13
  • 55
  • 68

3 Answers3

2

Rather than dot notation, you use brackets

res.json()["pic2"]["Pos"][0]
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

In your code, res.json() returns a dict of dicts. So all you have to to is to store the result of res.json() in a variable (I assume you're going to need other values from it, and it will make inspection / debugging easier anyway) and use the normal python dict/list access syntax.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
-2

This is one way to deal with this issue, it will be used addict package to handle this problem.

addict is a Python module that gives you dictionaries whose values are both gettable and settable using attributes, in addition to standard item-syntax

...

Installing

You can install via pip

pip install addict

or through conda

conda install addict -c conda-forge

Solution 1 :

import json
a ={"pic1": {"class_name": "dog1", "Pos": [11.11, 22.22, 33.33, 44.44], "Confidence": 0.98},
    "pic2": {"class_name": "dog2", "Pos": [99.99, 88.88, 77.77, 66.66], "Confidence": 0.99},
    "pic3": {"class_name": "dog3", "Pos": [33.33, 44.44, 55.55, 66.66], "Confidence": 0.97},
}
loaded_json = json.loads(json.dumps(a, indent=4, sort_keys=True))


from addict import Dict
mapping = Dict(loaded_json)

#print(mapping.pic1.Pos)  ## Get data
#mapping.pic1.Pos[0]="aa" ## Set data 
#print(mapping.pic1.Pos)
#print(mapping.pic2.Pos[0]) ## it will be show 99.99 

for i in mapping:
    #print (mapping[i]['Pos'])
    #print(mapping[i])
    for j in mapping[i]['Pos']:
        print(j)

reference Parse Json string in C#

Willie Cheng
  • 7,679
  • 13
  • 55
  • 68
  • What? The functionality that isn't commented out doesn't require `addict` at all. If you were just asking *"how do I access a dictionary key as an attribute?"*, see e.g. https://stackoverflow.com/q/4984647/3001761. And the `json.loads(json.dumps(...))` is just as pointless here as in the question. – jonrsharpe Sep 29 '19 at 16:00
  • @jonrsharpe thanks again, but my question is the data's from API(JSON data) so that I have tried to show what I got with JSon's data and I have to similar the same problem (JSON string) into my question content. sorry Not "how do I access a dictionary key as an attribute?", it isn't I want. – Willie Cheng Sep 29 '19 at 16:32
  • 1
    Well given that that's the only thing `addict` gives you over the vanilla dictionary you already had, I really don't understand how this answers the question. Parsing the string makes sense if that's what you're receiving, but you could just start with the dictionary here - dumping and loading it again is a no-op. – jonrsharpe Sep 29 '19 at 16:34
  • @jonrsharpe thanks very much for your response, I will be modifying my question and answer, anyway, thank. – Willie Cheng Sep 29 '19 at 16:37
  • *"this package can transfer JSON data's to Dictionary"* - no, it can't. That's what `json.loads` is doing - `loaded_json` is *already* a dictionary. All wrapping it in `addict.Dict` does is let you access the keys as attributes, e.g. `mapping.pic1` instead of `mapping['pic1']` (which you don't do in any of the code you haven't commented out...). – jonrsharpe Sep 29 '19 at 17:02
  • @jonrsharpe I have changed my content that is from the reference [GitHub](https://github.com/mewwts/addict). Hopefully, it can be more clear about the answer. thanks – Willie Cheng Sep 29 '19 at 17:22
  • You're still not actually using the features of that library at all, though, so using it **doesn't solve the problem**. I am certain I don't understand what your question actually is, but now I'm wondering if *you* do. – jonrsharpe Sep 29 '19 at 17:24