简体   繁体   中英

Json library in python StringIO parsing?

I just want to ask something about json and python.

If I'm correct, loading a StringIO to json would result to it having a u' .

for example, in the json library:

>>> from StringIO import StringIO
>>> io = StringIO('["streaming API"]')
>>> json.load(io)

results to

[u'streaming API']

so how do I parse this data? in order for me to sort a data that looks like this? I have this data I want to parse.

{u'brix': {u'Nov 29, 2017 11:20:15 PM': {u'Checklist': {u'Coffee tray with 3 coffee sticks, 3 creamer, 3 white sugar, 3 brown sugar, 1 equal or sweetener, 3 lipton tea, 2 mineral water, 3 cocktail napkin ?': u'No', u'Luggage bench fabric top is clean': u'No', u'1 facial tissue in a tissue box': u'No', u'Towel Reminder': u'No', u'1 pringles, 1 cashew nut, 1 cup noodles (placed in the coffee tray on the writing desk)?': u'No',

After reading a bit about json I found out that this may be a StringIO instead of a json. It was silly that I tried to do json.loads with it when I it's already loaded. All I want is to sort all of these data so it appears properly on web.

How do i properly do this? Do i decode these to json first so it becomes something like

{
 "maps":[
         {"id":"blabla","iscategorical":"0"},
         {"id":"blabla","iscategorical":"0"}
        ],
"masks":
         {"id":"valore"},
"om_points":"value",
"parameters":
         {"id":"valore"}
} 

Then get a value from that using this?

data["maps"][0]["id"]  # will return 'blabla'
data["masks"]["id"]    # will return 'valore'
data["om_points"]      # will return 'value'

Anyway, I'm confused of what I should do, I tried pretty much everything, these example came from other questions .

Here's a bit of my code:

result1 = firebase.get('/Rooms/Room1/2017-11-29/Inspection/Scan-in/Inspector/', None)
result2 = firebase.get('/Rooms/Room1/2017-11-29/Inspection/Scan-out/Inspector/', None)
print result1["brix"]

Firebase is supposed to return a json file. lol but it doesn't work on mine.

Thankyou very much to those who will clear it out to me.

you are receiving a Unicode json just as the comment says, if you want the output there's no need to convert to normal function. You can simply do something like this:

result1 = firebase.get('/Rooms/Room1/2017-11-29/Inspection/Scan-in/Inspector/', None)
result2 = firebase.get('/Rooms/Room1/2017-11-29/Inspection/Scan-out/Inspector/', None)
result1 = result1["brix"]["Nov 29, 2017 11:20:15 PM"]["Checklist"]["Luggage bench fabric top is clean"]

You will be receiving the

No

string, without the 'u

Here's the reference: https://docs.python.org/2/library/json.html

Look at the Decoding JSON part, there's the StringIO import, refer there for more information if you haven't read it yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM