我正在做API测试。所以我对json有一些回应。我想从中获取所有“名字”。为了这个目标,我使用了jsonpath_rw工具。但是在解析过程中,我得到了很多我不需要的额外信息。
这是我的回复:
"_embedded": {
"customer": [
{
"firstName": "Eve",
"lastName": "Berger",
"username": "Eve_Berger",
"id": "57a98d98e4b00679b4a830af",
"_links": {
"addresses": {
"href": "http://user/customers/57a98d98e4b00679b4a830af/addresses"
},
"cards": {
"href": "http://user/customers/57a98d98e4b00679b4a830af/cards"
},
"customer": {
"href": "http://user/customers/57a98d98e4b00679b4a830af"
},
"self": {
"href": "http://user/customers/57a98d98e4b00679b4a830af"
}
}
},
{
"firstName": "User",
"lastName": "Name",
"username": "user",
"id": "57a98d98e4b00679b4a830b2",
"_links": {
"addresses": {
"href": "http://user/customers/57a98d98e4b00679b4a830b2/addresses"
},
"cards": {
"href": "http://user/customers/57a98d98e4b00679b4a830b2/cards"
},
"customer": {
"href": "http://user/customers/57a98d98e4b00679b4a830b2"
},
"self": {
"href": "http://user/customers/57a98d98e4b00679b4a830b2"
}
}
},
{
"firstName": "TestName",
"lastName": "TestSurname",
"username": "test12",
"id": "5cf7f4bcee11cb0001ecdb07",
"_links": {
"addresses": {
"href": "http://user/customers/5cf7f4bcee11cb0001ecdb07/addresses"
},
"cards": {
"href": "http://user/customers/5cf7f4bcee11cb0001ecdb07/cards"
},
"customer": {
"href": "http://user/customers/5cf7f4bcee11cb0001ecdb07"
},
"self": {
"href": "http://user/customers/5cf7f4bcee11cb0001ecdb07"
}
}
}
]
}
}
我希望解析所有“ firstName”值,并且仅看到这样的结果:[“ Eve”,“ User”,“ TestName”] 当我使用json在这里(http://jsonpath.herokuapp.com)尝试并使用路径“ $ .. firstName”时,它可以正常工作。 但是在我的Python代码中,它看起来不太好,并且对大量额外数据的响应如此之大。 有人可以帮我解决什么吗?
因为现在解析后我的响应看起来与我期望的不同。我只想看到这三个名称(“ Eve”,“ User”,“ TestName”),但这就是我现在拥有的名称:
DatumInContext(value='Eve',
path=Fields('firstName'),
context=DatumInContext(value={
'firstName': 'Eve',
'lastName': 'Berger',
'username': 'Eve_Berger',
'id': '57a98d98e4b00679b4a830af',
'_links': {
'addresses': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af/addresses'
},
'cards': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af/cards'
},
'customer': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af'
},
'self': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af'
}
}
},
path=<jsonpath_rw.jsonpath.Indexobjectat0x10620a5f8>,
context=DatumInContext(value=[
{
'firstName': 'Eve',
'lastName': 'Berger',
'username': 'Eve_Berger',
'id': '57a98d98e4b00679b4a830af',
'_links': {
'addresses': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af/addresses'
},
'cards': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af/cards'
},
'customer': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af'
},
'self': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af'
}
}
},
{
'firstName': 'User',
'lastName': 'Name',
'username': 'user',
'id': '57a98d98e4b00679b4a830b2',
'_links': {
'addresses': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2/addresses'
},
'cards': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2/cards'
},
'customer': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2'
},
'self': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2'
}
}
},
{
'firstName': 'TestName',
'lastName': 'TestSurname',
'username': 'test12',
'id': '5cf7f4bcee11cb0001ecdb07',
'_links': {
'addresses': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07/addresses'
},
'cards': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07/cards'
},
'customer': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07'
},
'self': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07'
}
}
}
],
path=Fields('customer'),
context=DatumInContext(value={
'customer': [
{
'firstName': 'Eve',
'lastName': 'Berger',
'username': 'Eve_Berger',
'id': '57a98d98e4b00679b4a830af',
'_links': {
'addresses': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af/addresses'
},
'cards': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af/cards'
},
'customer': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af'
},
'self': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af'
}
}
},
{
'firstName': 'User',
'lastName': 'Name',
'username': 'user',
'id': '57a98d98e4b00679b4a830b2',
'_links': {
'addresses': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2/addresses'
},
'cards': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2/cards'
},
'customer': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2'
},
'self': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2'
}
}
},
{
'firstName': 'TestName',
'lastName': 'TestSurname',
'username': 'test12',
'id': '5cf7f4bcee11cb0001ecdb07',
'_links': {
'addresses': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07/addresses'
},
'cards': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07/cards'
},
'customer': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07'
},
'self': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07'
}
}
}
]
},
path=Fields('_embedded'),
context=DatumInContext(value={
'_embedded': {
'customer': [
{
'firstName': 'Eve',
'lastName': 'Berger',
'username': 'Eve_Berger',
'id': '57a98d98e4b00679b4a830af',
'_links': {
'addresses': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af/addresses'
},
'cards': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af/cards'
},
'customer': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af'
},
'self': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af'
}
}
},
{
'firstName': 'User',
'lastName': 'Name',
'username': 'user',
'id': '57a98d98e4b00679b4a830b2',
'_links': {
'addresses': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2/addresses'
},
'cards': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2/cards'
},
'customer': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2'
},
'self': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2'
}
}
},
{
'firstName': 'TestName',
'lastName': 'TestSurname',
'username': 'test12',
'id': '5cf7f4bcee11cb0001ecdb07',
'_links': {
'addresses': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07/addresses'
},
'cards': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07/cards'
},
'customer': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07'
},
'self': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07'
}
}
}
]
}
},
path=Root(),
context=None))))),
DatumInContext(value='User',
path=Fields('firstName'),
context=DatumInContext(value={
'firstName': 'User',
'lastName': 'Name',
'username': 'user',
'id': '57a98d98e4b00679b4a830b2',
'_links': {
'addresses': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2/addresses'
},
'cards': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2/cards'
},
'customer': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2'
},
'self': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2'
}
}
},
path=<jsonpath_rw.jsonpath.Indexobjectat0x106245908>,
context=DatumInContext(value=[
{
'firstName': 'Eve',
'lastName': 'Berger',
'username': 'Eve_Berger',
'id': '57a98d98e4b00679b4a830af',
'_links': {
'addresses': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af/addresses'
},
'cards': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af/cards'
},
'customer': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af'
},
'self': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af'
}
}
},
{
'firstName': 'User',
'lastName': 'Name',
'username': 'user',
'id': '57a98d98e4b00679b4a830b2',
'_links': {
'addresses': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2/addresses'
},
'cards': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2/cards'
},
'customer': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2'
},
'self': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2'
}
}
},
{
'firstName': 'TestName',
'lastName': 'TestSurname',
'username': 'test12',
'id': '5cf7f4bcee11cb0001ecdb07',
'_links': {
'addresses': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07/addresses'
},
'cards': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07/cards'
},
'customer': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07'
},
'self': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07'
}
}
}
],
path=Fields('customer'),
context=DatumInContext(value={
'customer': [
{
'firstName': 'Eve',
'lastName': 'Berger',
'username': 'Eve_Berger',
'id': '57a98d98e4b00679b4a830af',
'_links': {
'addresses': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af/addresses'
},
'cards': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af/cards'
},
'customer': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af'
},
'self': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af'
}
}
},
{
'firstName': 'User',
'lastName': 'Name',
'username': 'user',
'id': '57a98d98e4b00679b4a830b2',
'_links': {
'addresses': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2/addresses'
},
'cards': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2/cards'
},
'customer': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2'
},
'self': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2'
}
}
},
{
'firstName': 'TestName',
'lastName': 'TestSurname',
'username': 'test12',
'id': '5cf7f4bcee11cb0001ecdb07',
'_links': {
'addresses': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07/addresses'
},
'cards': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07/cards'
},
'customer': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07'
},
'self': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07'
}
}
}
]
},
path=Fields('_embedded'),
context=DatumInContext(value={
'_embedded': {
'customer': [
{
'firstName': 'Eve',
'lastName': 'Berger',
'username': 'Eve_Berger',
'id': '57a98d98e4b00679b4a830af',
'_links': {
'addresses': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af/addresses'
},
'cards': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af/cards'
},
'customer': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af'
},
'self': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af'
}
}
},
{
'firstName': 'User',
'lastName': 'Name',
'username': 'user',
'id': '57a98d98e4b00679b4a830b2',
'_links': {
'addresses': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2/addresses'
},
'cards': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2/cards'
},
'customer': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2'
},
'self': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2'
}
}
},
{
'firstName': 'TestName',
'lastName': 'TestSurname',
'username': 'test12',
'id': '5cf7f4bcee11cb0001ecdb07',
'_links': {
'addresses': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07/addresses'
},
'cards': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07/cards'
},
'customer': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07'
},
'self': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07'
}
}
}
]
}
},
path=Root(),
context=None))))),
DatumInContext(value='TestName',
path=Fields('firstName'),
context=DatumInContext(value={
'firstName': 'TestName',
'lastName': 'TestSurname',
'username': 'test12',
'id': '5cf7f4bcee11cb0001ecdb07',
'_links': {
'addresses': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07/addresses'
},
'cards': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07/cards'
},
'customer': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07'
},
'self': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07'
}
}
},
path=<jsonpath_rw.jsonpath.Indexobjectat0x106245cf8>,
context=DatumInContext(value=[
{
'firstName': 'Eve',
'lastName': 'Berger',
'username': 'Eve_Berger',
'id': '57a98d98e4b00679b4a830af',
'_links': {
'addresses': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af/addresses'
},
'cards': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af/cards'
},
'customer': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af'
},
'self': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af'
}
}
},
{
'firstName': 'User',
'lastName': 'Name',
'username': 'user',
'id': '57a98d98e4b00679b4a830b2',
'_links': {
'addresses': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2/addresses'
},
'cards': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2/cards'
},
'customer': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2'
},
'self': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2'
}
}
},
{
'firstName': 'TestName',
'lastName': 'TestSurname',
'username': 'test12',
'id': '5cf7f4bcee11cb0001ecdb07',
'_links': {
'addresses': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07/addresses'
},
'cards': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07/cards'
},
'customer': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07'
},
'self': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07'
}
}
}
],
path=Fields('customer'),
context=DatumInContext(value={
'customer': [
{
'firstName': 'Eve',
'lastName': 'Berger',
'username': 'Eve_Berger',
'id': '57a98d98e4b00679b4a830af',
'_links': {
'addresses': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af/addresses'
},
'cards': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af/cards'
},
'customer': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af'
},
'self': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af'
}
}
},
{
'firstName': 'User',
'lastName': 'Name',
'username': 'user',
'id': '57a98d98e4b00679b4a830b2',
'_links': {
'addresses': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2/addresses'
},
'cards': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2/cards'
},
'customer': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2'
},
'self': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2'
}
}
},
{
'firstName': 'TestName',
'lastName': 'TestSurname',
'username': 'test12',
'id': '5cf7f4bcee11cb0001ecdb07',
'_links': {
'addresses': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07/addresses'
},
'cards': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07/cards'
},
'customer': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07'
},
'self': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07'
}
}
}
]
},
path=Fields('_embedded'),
context=DatumInContext(value={
'_embedded': {
'customer': [
{
'firstName': 'Eve',
'lastName': 'Berger',
'username': 'Eve_Berger',
'id': '57a98d98e4b00679b4a830af',
'_links': {
'addresses': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af/addresses'
},
'cards': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af/cards'
},
'customer': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af'
},
'self': {
'href': 'http://user/customers/57a98d98e4b00679b4a830af'
}
}
},
{
'firstName': 'User',
'lastName': 'Name',
'username': 'user',
'id': '57a98d98e4b00679b4a830b2',
'_links': {
'addresses': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2/addresses'
},
'cards': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2/cards'
},
'customer': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2'
},
'self': {
'href': 'http://user/customers/57a98d98e4b00679b4a830b2'
}
}
},
{
'firstName': 'TestName',
'lastName': 'TestSurname',
'username': 'test12',
'id': '5cf7f4bcee11cb0001ecdb07',
'_links': {
'addresses': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07/addresses'
},
'cards': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07/cards'
},
'customer': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07'
},
'self': {
'href': 'http://user/customers/5cf7f4bcee11cb0001ecdb07'
}
}
}
]
}
},
path=Root(),
context=None)))))
]
这是我的Python代码:
from jsonpath_rw import parse
def match(self, response):
json = response.json()
value = parse("$..firstName").find(json)
print(json)
print(value)
print(len(value))
答案 0 :(得分:0)
value是一个包含所有找到的对象的列表。列表中的每个项目都有一个带有结果的值,如果什么都没有找到,则该值为空。
print([x.value for x in found])
结果:
['Eve', 'User', 'TestName']
希望这会有所帮助。