简体   繁体   English

Python:解析单行JSON文件

[英]Python: Parsing a single-line JSON file

I'm trying to parse a large one-line JSON file and can't seem to figure it out. 我正在尝试解析一个大的单行JSON文件,似乎无法弄清楚。 I've looked for resources on here and elsewhere, but most of what I see tells you to parse everything line-by-line. 我一直在这里和其他地方寻找资源,但是我看到的大部分内容都告诉您逐行解析所有内容。 Since I'm working with one really long line, what's the best way to parse this with python? 由于我正在处理很长的一行,因此用python解析这条线的最佳方法是什么?

Specifically, I'm looking for one particular value that's in a nested dictionary in the JSON data. 具体来说,我正在寻找JSON数据的嵌套字典中的一个特定值。 The data looks like this (after running it through a formatter): 数据如下所示(通过格式化程序运行之后):

{
   "type":"FeatureCollection",
   "features":[
      {
         "type":"Feature",
         "id":"wells.529038",
         "geometry":null,
         "properties":{
            "api":"4245180382"
         }
      },
      {
         "type":"Feature",
         "id":"wells.481699",
         "geometry":null,
         "properties":{
            "api":"4237182573"
         }
      }
   ]
}

I want to extract all the api values, but I'm having trouble wrapping my head around how to do that given the multi-nested structure and because the file is huge and only one line. 我想提取所有的api值,但是在给定多嵌套结构的情况下,由于文件很大且只有一行,因此我难以解决该问题。 What the best approach here? 这里最好的方法是什么?

Use the standard library : 使用标准库:

json_data = json.loads(your_line)

# Usage exemple
for feature in json_data['features']:
    print feature['id']

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

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