简体   繁体   English

Python-SimpleJSON问题

[英]Python - SimpleJSON Issue

I'm working with the Mega API and Python in hope to produce a folder tree readable by Python. 我正在使用Mega API和Python,希望产生一个Python可读的文件夹树。 At the moment I'm working with the JSON responses Mega's API gives, but for some reason am having trouble parsing it. 目前,我正在使用Mega的API提供的JSON响应,但由于某种原因,在解析它时遇到了麻烦。 In the past I would simply use simplejson in the format below, though right now it's not working. 过去,我只是以下面的格式使用simplejson,尽管现在它不起作用。 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. 这是对使用Mega API搜索文件的响应。 I'm using the module found here. 我正在使用这里找到的模块。 https://github.com/richardasaurus/mega.py The code itself looks like this: https://github.com/richardasaurus/mega.py代码本身看起来像这样:

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: m.find中获得的东西只是一个python元组,其中第1个元素(在第0个元素之后)是一个字典:

(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. 因此,根本不需要使用simplejson

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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