【问题标题】:parse json file ValueError解析json文件ValueError
【发布时间】:2015-07-30 14:48:50
【问题描述】:

您好,我有一个 json 文件并想解析它。我写了以下代码:

import json as simplejson
import os

for filename in os.listdir('/home/Documents/test/'):
    with open('/home/Documents/test/' + filename) as file:
            data = simplejson.load(file)
            try:
                if(data['scans']['Microsoft']['detected']==True):
                    label = data['scans']['Microsoft']['result']
                    print label
            except (ValueError, KeyError, TypeError):
                print "JSON format error"

但我收到以下错误:

ValueError: Expecting property name: line 1 column 1 (char 1)

你能帮我解决什么问题吗?我认为是因为我的文件包含 unicode 字符,但我不知道该如何解决。

这是我的 json 文件的一部分:

{u'md5': u'0a1cdc568b4da42cb7acce45834eb4ba',
 u'permalink': u'https://www.virustotal.com/file/a0acc0feb1da7f571faaa7dc3b7ebcd1b856710d1f44cd6e0b57ec5a9bc70038/analysis/1383102973/',
 u'positives': 42,
 u'resource': u'0A1CDC568B4DA42CB7ACCE45834EB4BA',
 u'response_code': 1,
 u'scan_date': u'2013-10-30 03:16:13',
 u'scan_id': u'a0acc0feb1da7f571faaa7dc3b7ebcd1b856710d1f44cd6e0b57ec5a9bc70038-1383102973',
 u'scans': {u'AVG': {u'detected': True,
                     u'result': u'FakeAV.AFQQ',
                     u'update': u'20131029',
                     u'version': u'13.0.0.3169'},
            u'Agnitum': {u'detected': True,
                         u'result': u'FraudTool.Agent!dfdcHBsNM3c',
                         u'update': u'20131029',
                         u'version': u'5.5.1.3'},
            u'AhnLab-V3': {u'detected': True,
                           u'result': u'Trojan/Win32.FakeAV',
                           u'update': u'20131029',
                           u'version': u'2013.10.30.01'},
            u'AntiVir': {u'detected': True,
                         u'result': u'TR/Winwebsec.bamnx',
                         u'update': u'20131030',
                         u'version': u'7.11.110.26'},
            u'Antiy-AVL': {u'detected': False,
                           u'result': None,
                           u'update': u'20131029',
                           u'version': u'2.0.3.7'},

【问题讨论】:

  • 您没有 JSON。你有 Python 文字。
  • 你这是什么意思?我的文件有 json 扩展名..
  • 修复创建文件的程序(不要使用pprint.pformat(),使用json.dumps())或使用ast.literal_eval()从该字符串生成Python对象。
  • @Alex:仅仅因为它具有 .json 扩展名并不意味着它是实际的 JSON 数据。它不是。它包含 Python 2 文字语法。
  • @Martijn:感谢您的信息。我认为这是一个标准的 json 文件,因为我是从病毒总网站获得的。。

标签: python json python-2.7


【解决方案1】:

您没有 JSON 数据。你有 Python 字面量(Python 语法定义字典、布尔值、字符串和 None 对象,就像你在 Python 源代码中找到的一样)。

JSON 数据不使用单引号作为字符串,也不以 u 为前缀,但 Python 2 的 unicode 对象。 JSON 使用 null,而 Python 使用 Nonefalsetrue,而 Python 使用 FalseTrue

要么修复创建文件的过程(它看起来像 pprint 模块将产生的输出)以实际生成 JSON,要么使用 ast.literal_eval() function 将字符串安全地转换回 Python 对象。

如果您使用the scripts from this repository,请知道--jsondump 开关不会产生 JSON 输出。脚本将来自 API 的 JSON 加载到 Python 结构中,而开关只是将该结构转储到文件中作为 Python 文字

if jsondump == True:
  jsondumpfile = open("VTDL" + md5 + ".json", "w")
  pprint(it, jsondumpfile)
  jsondumpfile.close()
  print "\n\tJSON Written to File -- " + "VTDL" + md5 + ".json"

请注意那里的pprint() 呼叫。如果您需要将 JSON 加载到自己的 Python 代码中,请编写自己的 API 调用,而不是依赖此脚本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-09
    • 2016-06-19
    • 1970-01-01
    • 2015-08-20
    • 2018-10-10
    • 2019-06-09
    • 2020-04-12
    • 1970-01-01
    相关资源
    最近更新 更多