使用Python从JSON文件解析数据

时间:2021-09-25 15:33:03

I have a JSON file output as below

我有一个JSON文件输出,如下所示

{
  u'_embedded': {
u'workouts': [
  {
    u'start_datetime': u'2015-02-16T08:47:00+00:00',
    u'name': u'Walk',
    u'updated_datetime': u'2015-02-16T19:20:36+00:00',
    u'created_datetime': u'2015-02-16T19:20:36+00:00',
    u'notes': u'',
    u'reference_key': None,
    u'start_locale_timezone': u'Europe/London',
    u'source': None,
    u'_links': {
      u'user': [
        {
          u'href': u'/v7.1/user/******/',
          u'id': u'******'
        }
      ],
      u'self': [
        {
          u'href': u'/v7.1/workout/*******/',
          u'id': u'*******'
        }
      ],
      u'privacy': [
        {
          u'href': u'/v7.1/privacy_option/1/',
          u'id': u'1'
        }
      ],
      u'route': [
        {
          u'href': u'/v7.1/route/*******/',
          u'id': u'*******'
        }
      ],
      u'activity_type': [
        {
          u'href': u'/v7.1/activity_type/9/',
          u'id': u'9'
        }
      ]
    },
    u'has_time_series': False,
    u'is_verified': False,
    u'aggregates': {
      u'active_time_total': 1020.0,
      u'elapsed_time_total': 1020.0,
      u'distance_total': 1729.99651968,
      u'speed_avg': 1.6972231232,
      u'steps_total': 0.0
    }
  }
]
  },
  u'_links': {
u'self': [
  {
    u'href': u'/v7.1/workout/?limit=20&user=******&offset=0'
  }
],
u'documentation': [
  {
    u'href': u'https://developer.underarmour.com/docs/v71_Workout'
  }
]
  },
  u'total_count': 1
}

From this code, I wish to pull the workout id on it's own, which is in the

从这段代码中,我希望将锻炼id单独提取出来,它在

u'user': [
    {
      u'href': u'/v7.1/user/******/',
      u'id': u'******'
    } 

I've managed it using json.loads in a different piece of code, although as in my code below, I am not sure how to implement it so I can use json.loads instead of x.json() as I am using, so I've become lost. I've attempted to do it as I would using json.loads by using indexes, but it just throws an error. Below is a snippet of the code and what I have attempted

我使用json管理它。加载在不同的代码中,尽管在下面的代码中,我不确定如何实现它,这样我就可以使用json了。我使用的是load而不是x.json(),因此我已经丢失了。我尝试过使用json。使用索引加载,但它只会抛出一个错误。下面是代码片段和我尝试的内容

route = requests.get(url='https://oauth2-api.mapmyapi.com/v7.1/workout/?user=********', verify=False,
                    headers={'api-key': CLIENT_ID, 'authorization': 'Bearer %s' % access_token['access_token']})

route = route.json()

for info in route['workouts']['self']:
print info['id']

#print route

Any help as to how to pull the data from the JSON would be hugely appreciated, or how to edit my code to allow me to use json.loads instead.

任何关于如何从JSON中提取数据的帮助,或者如何编辑代码以允许我使用JSON的帮助,都会受到极大的赞赏。加载。

Thanks.

谢谢。

3 个解决方案

#1


0  

As it's a python dict object you use proper keys to get the values. From the dictionary you provided, if the the dictionary is d then.

因为它是一个python dict类型的对象,所以您使用适当的键来获取值。从你提供的字典中,如果字典是d。

itm = d.get(u'_embedded').get(u'workouts')[0]

itm is,

itm是,

{u'_links': {u'activity_type': [{u'href': u'/v7.1/activity_type/9/',
    u'id': u'9'}],
  u'privacy': [{u'href': u'/v7.1/privacy_option/1/', u'id': u'1'}],
  u'route': [{u'href': u'/v7.1/route/*******/', u'id': u'*******'}],
  u'self': [{u'href': u'/v7.1/workout/*******/', u'id': u'*******'}],
  u'user': [{u'href': u'/v7.1/user/******/', u'id': u'******'}]},
 u'aggregates': {u'active_time_total': 1020.0,
  u'distance_total': 1729.99651968,
  u'elapsed_time_total': 1020.0,
  u'speed_avg': 1.6972231232,
  u'steps_total': 0.0},
 u'created_datetime': u'2015-02-16T19:20:36+00:00',
 u'has_time_series': False,
 u'is_verified': False,
 u'name': u'Walk',
 u'notes': u'',
 u'reference_key': None,
 u'source': None,
 u'start_datetime': u'2015-02-16T08:47:00+00:00',
 u'start_locale_timezone': u'Europe/London',
 u'updated_datetime': u'2015-02-16T19:20:36+00:00'}

get the user information:

获取用户信息:

 print itm.get(u'_links').get(u'user')

 [{u'href': u'/v7.1/user/******/', u'id': u'******'}]

now get get id:

现在得到id:

print itm.get(u'_links').get(u'user')[0].get(u'id')
******

You might iterate over the list instead of using index value.

您可以对列表进行迭代,而不是使用索引值。

#2


1  

That is not JSON. Its a Python dictionary. Do you see: single quotes for strings and u'blah blah'? That's not JSON. They are python object representations.

这不是JSON。Python字典。你看到了吗,字符串的单引号和u'blah '?这不是JSON。它们是python对象表示。

Use ast.literal_eval to parse everything back from a string to a python object.

使用ast.literal_eval将所有东西从字符串解析回python对象。

>>> import ast
>>> with open('input') as f:
...     obj = ast.literal_eval(f.read())
...
>>> obj
{u'_links': ......... } #Your entire object appears here.
>>> for item in obj["_embedded"]["workouts"]:
...     print item["_links"]["user"]
...
[{u'href': u'/v7.1/user/******/', u'id': u'******'}]

#3


0  

That is already a Python dict. It is not JSON and there is no need to load it.

这已经是一个Python命令了,它不是JSON,也不需要加载它。

#1


0  

As it's a python dict object you use proper keys to get the values. From the dictionary you provided, if the the dictionary is d then.

因为它是一个python dict类型的对象,所以您使用适当的键来获取值。从你提供的字典中,如果字典是d。

itm = d.get(u'_embedded').get(u'workouts')[0]

itm is,

itm是,

{u'_links': {u'activity_type': [{u'href': u'/v7.1/activity_type/9/',
    u'id': u'9'}],
  u'privacy': [{u'href': u'/v7.1/privacy_option/1/', u'id': u'1'}],
  u'route': [{u'href': u'/v7.1/route/*******/', u'id': u'*******'}],
  u'self': [{u'href': u'/v7.1/workout/*******/', u'id': u'*******'}],
  u'user': [{u'href': u'/v7.1/user/******/', u'id': u'******'}]},
 u'aggregates': {u'active_time_total': 1020.0,
  u'distance_total': 1729.99651968,
  u'elapsed_time_total': 1020.0,
  u'speed_avg': 1.6972231232,
  u'steps_total': 0.0},
 u'created_datetime': u'2015-02-16T19:20:36+00:00',
 u'has_time_series': False,
 u'is_verified': False,
 u'name': u'Walk',
 u'notes': u'',
 u'reference_key': None,
 u'source': None,
 u'start_datetime': u'2015-02-16T08:47:00+00:00',
 u'start_locale_timezone': u'Europe/London',
 u'updated_datetime': u'2015-02-16T19:20:36+00:00'}

get the user information:

获取用户信息:

 print itm.get(u'_links').get(u'user')

 [{u'href': u'/v7.1/user/******/', u'id': u'******'}]

now get get id:

现在得到id:

print itm.get(u'_links').get(u'user')[0].get(u'id')
******

You might iterate over the list instead of using index value.

您可以对列表进行迭代,而不是使用索引值。

#2


1  

That is not JSON. Its a Python dictionary. Do you see: single quotes for strings and u'blah blah'? That's not JSON. They are python object representations.

这不是JSON。Python字典。你看到了吗,字符串的单引号和u'blah '?这不是JSON。它们是python对象表示。

Use ast.literal_eval to parse everything back from a string to a python object.

使用ast.literal_eval将所有东西从字符串解析回python对象。

>>> import ast
>>> with open('input') as f:
...     obj = ast.literal_eval(f.read())
...
>>> obj
{u'_links': ......... } #Your entire object appears here.
>>> for item in obj["_embedded"]["workouts"]:
...     print item["_links"]["user"]
...
[{u'href': u'/v7.1/user/******/', u'id': u'******'}]

#3


0  

That is already a Python dict. It is not JSON and there is no need to load it.

这已经是一个Python命令了,它不是JSON,也不需要加载它。