سؤال

I'm working with the Mega API and Python in hope to produce a folder tree readable by Python. At the moment I'm working with the JSON responses Mega's API gives, but for some reason am having trouble parsing it. In the past I would simply use simplejson in the format below, though right now it's not working. At the moment I'm just trying to get the file name. Any help is appreciated!

import simplejson    

megaResponseToFileSearch = "(u'BMExefXbYa', {u'a': {u'n': u'A Bullet For Pretty Boy - 1 - Dial M For Murder.mp3'}, u'h': u'BMExXbYa', u'k': (5710166, 21957970, 11015946, 7749654L), u'ts': 13736999, 'iv': (7949460, 15946811, 0, 0), u'p': u'4FlnwBTb', u's': 5236864, 'meta_mac': (529642, 2979591L), u'u': u'xpz_tb-YDUg', u't': 0, 'key': (223xx15874, 642xx8505, 1571620, 26489769L, 799460, 1596811, 559642, 279591L)})"

jsonRespone = simplejson.loads(megaResponseToFileSearch)

print jsonRespone[u'a'][u'n']

ERROR:

Traceback (most recent call last):
  File "D:/Projects/Mega Sync/megasync.py", line 18, in <module>
    jsonRespone = simplejson.loads(file4)
  File "D:\Projects\Mega Sync\simplejson\__init__.py", line 453, in loads
    return _default_decoder.decode(s)
  File "D:\Projects\Mega Sync\simplejson\decoder.py", line 429, in decode
    obj, end = self.raw_decode(s)
  File "D:\Projects\Mega Sync\simplejson\decoder.py", line 451, in raw_decode
    raise JSONDecodeError("No JSON object could be decoded", s, idx)
simplejson.decoder.JSONDecodeError: No JSON object could be decoded: line 1 column 0 (char 0)

EDIT:

I was asked where I got the string from. It's a response to searching for a file using the Mega API. I'm using the module found here. https://github.com/richardasaurus/mega.py The code itself looks like this:

from mega import Mega
mega = Mega({'verbose': True})
m = mega.login(email, password)
file = m.find('A Bullet For Pretty Boy - 1 - Dial M For Murder.mp3')
print file
هل كانت مفيدة؟

المحلول

The thing you are getting from m.find is just a python tuple, where the 1-st (next after the 0th) element is a dictionary:

(u'99M1Tazb',
 {u'a': {u'n': u'test.txt'}, 
  u'h': u'99M1Tazb', 
  u'k': (1145485578, 1435138417, 702505527, 274874292), 
  u'ts': 1373482712,
  'iv': (1883603069, 763415510, 0, 0), 
  u'p': u'9td12YaY', 
  u's': 0, 
  'meta_mac': (1091379956, 402442960),
  u'u': u'79_166PAQCA', 
  u't': 0,
  'key': (872626551, 2013967015, 1758609603, 127858020, 1883603069, 763415510, 1091379956, 402442960)})

To get the filename, just use:

print file[1]['a']['n'] 

So, no need to use simplejson at all.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top