简体   繁体   English

ast.literal_eval引发ValueError

[英]ast.literal_eval throwing ValueError

ast.literal_eval is throwing ValueError: malformed string for the following JSON. ast.literal_eval抛出ValueError: malformed string以下JSON的ValueError: malformed string I am unable to spot what the error is. 我无法发现错误所在。 I used online JSON validators, all mention that this a valid JSON. 我使用了在线JSON验证程序,所有人都提到这是有效的JSON。

Can someone help me spot the error with this string? 有人可以帮我找出此字符串的错误吗?

{
    "business_id": "Iu-oeVzv8ZgP18NIB0UMqg",
    "full_address": "3320 S Hill St,South East LA,Los Angeles, CA 90007",
    "schools": [
        "University of Southern California"
    ],
    "open": true,
    "categories": [
        "Medical Centers",
        "Health and Medical"
    ],
    "photo_url": "http://s3-media1.ak.yelpcdn.com/bphoto/SdUWxREuWuPvvot6faxfXg/ms.jpg",
    "city": "Los Angeles",
    "review_count": 2,
    "name": "Southern California Medical Group",
    "neighborhoods": [
        "South East LA"
    ],
    "url": "http://www.yelp.com/biz/southern-california-medical-group-los-angeles",
    "longitude": -118.274281,
    "state": "CA",
    "stars": 3.5,
    "latitude": 34.01971,
    "type": "business"
}

true is an invalid literal in Python: true是Python中的无效文字:

In [2]: ast.literal_eval('true')
ValueError: malformed node or string: <_ast.Name object at 0x7f32a096d550>

Use json module (specifically json.loads ) when you need to deserialize a JSON document. 需要反序列化JSON文档时,请使用json模块(特别是json.loads )。

You're using the wrong tool for the job here - ast.literal_eval is intended for parsing python literals from strings into python objects. 您在此处使用了错误的工具ast.literal_eval用于将python文字从字符串解析为python对象。 It is not intended for loading data from serialised JSON into python (although it may also work for that in a subset of cases, because serialised json format happens to overlap with python syntax some of the time). 它不打算将数据从序列化的JSON加载到python中(尽管它在某些情况下也可能适用,因为序列化的json格式有时会与python语法重叠)。

Perhaps you were you looking for json.loads ? 也许您在寻找json.loads

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

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