首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法解析JSON文件中特定标记的所有实例

无法解析JSON文件中特定标记的所有实例
EN

Stack Overflow用户
提问于 2020-02-26 22:37:31
回答 1查看 26关注 0票数 0

我这里有一个JSON。问题是JSON有3个“描述”标签,但我只能读取第一条记录的标签一次。我不能从所有的“description”标签中移动到& read description。在我的代码中,我已经能够计算出3个“description”标签的实例,但不能全部读完。这里的For循环在所有3个位置重复第一个标记的内容。请帮帮忙。

代码:

代码语言:javascript
复制
import urllib.request, urllib.parse, urllib.error
from socket import timeout
import requests
import lxml
import json
import ssl

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

url = requests.get('http://someapi').text

newDictionary=json.loads(str(url))
x = newDictionary['data']['job'][0]['description']['translation']
y = newDictionary['data']['job'][0]['description']

count=0
for item in range(len(newDictionary['data']['job'])):
    count = count+1
    if x=='Null':
        print(y)
    else:
        print(x)
print (count)

示例JSON:

代码语言:javascript
复制
{
  "isError": false,
  "message": "SUCCESS",
  "statusCode": 200,
  "data": {
    "job": [
      {
        "dates": {
          "firstseen": "1991-01-13",
          "lastseen": "1991-02-23"
        },
        "title": {
          "value": "Holland(ALPHA) – Color Line",
          "translation": "Holland(ALPHA) - Color Line",
          "titleid": 896616466,
          "cleantitleid": 12087238,
          "semicleantitleid": 349366870
        },
        "description": {
          "value": "Holland(ALPHA) is in foreign language.",
          "translation": "Holland(ALPHA) is in English"
        },
        "requirements": {
          "value": "",
          "translation": null
        },
        "education": {
          "id": 5,
          "label": "Bachelor's degree"
        },
        "jobTypes": {
          "jobType": [
            {
              "id": "4",
              "label": "Full-Time"
            },
            {
              "id": "1",
              "label": "Permanent"
            }
          ]
        },
        "skillslist": {
          "element": [
            {
              "id": 30925,
              "label": "Negotiation",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "SOFT",
              "count": 0
            },
            {
              "id": 37810,
              "label": "Information technology",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 38451,
              "label": "Architecture",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 36710,
              "label": "Collaboration",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "SOFT",
              "count": 0
            },
            {
              "id": 36712,
              "label": "Best practices",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 33674,
              "label": "Budgeting",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "SOFT",
              "count": 0
            },
            {
              "id": 40628,
              "label": "Trust development",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "SOFT",
              "count": 0
            }
          ]
        },
        "seniority": {
          "id": 2,
          "label": "Mid-level "
        },
        "employer": {
          "id": "4485465",
          "name": "Color Line",
          "superaliasid": "4485465",
          "superalias": "Color Line",
          "translation": "Color Line"
        },
        "locations": {
          "location": [
            {
              "city": {
                "id": 3860366,
                "code": "",
                "label": "Oslo",
                "translation": "Oslo"
              },
              "state": {
                "id": 4419,
                "code": "",
                "acronym": null,
                "label": "Oslo and Akershus",
                "translation": "Oslo and Akershus"
              },
              "county": {
                "id": 0,
                "code": "",
                "label": "Unavailable"
              },
              "wib": {
                "id": 0,
                "code": "00000",
                "label": "Unavailable"
              },
              "msa": {
                "id": 8022,
                "code": "",
                "label": "Oslo",
                "translation": "Oslo"
              },
              "superregion": {
                "id": 0,
                "code": "00",
                "label": "Unavailable"
              },
              "country": {
                "id": 169,
                "code": "NO",
                "label": "Norway"
              },
              "position": {
                "latitude": "59.975399017333984",
                "longitude": "10.738800048828125"
              }
            }
          ]
        },
        "salaries": {
          "salary": [
            {
              "id": 2,
              "type": "Modeled",
              "value": 0
            }
          ]
        },
        "sources": {
          "source": [
            {
              "id": 136623,
              "jobid": "4187345693",
              "tags": "",
              "type": "4",
              "name": "CV Norway",
              "url": "XXX",
              "validlink": true
            }
          ]
        },
        "occupation": {
          "occupation": {
            "id": 10011,
            "code": "11302100",
            "label": "Computer and Information Systems ",
            "revision": "2011",
            "option": null
          }
        }
      },
      {
        "dates": {
          "firstseen": "1991-01-09",
          "lastseen": "1991-02-22"
        },
        "title": {
          "value": "Holland(H/F)",
          "translation": "Alpha",
          "titleid": 321624492,
          "cleantitleid": 12087238,
          "semicleantitleid": 36908
        },
        "description": {
          "value": "Accueil » HOLLAND(H/F) in French",
          "translation": "Home\" HOLLAND(H/F) in English
        },
        "requirements": {
          "value": "",
          "translation": null
        },
        "education": {
          "id": 5,
          "label": "Bachelor's degree"
        },
        "jobTypes": {
          "jobType": [
            {
              "id": "4",
              "label": "Full-Time"
            },
            {
              "id": "1",
              "label": "Permanent"
            }
          ]
        },
        "skillslist": {
          "element": [
            {
              "id": 34182,
              "label": "Responsibility",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "SOFT",
              "count": 0
            },
            {
              "id": 31350,
              "label": "Release management",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "SOFT",
              "count": 0
            },
            {
              "id": 44384,
              "label": "Writing",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "SOFT",
              "count": 0
            },
            {
              "id": 38029,
              "label": "High Availability",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 31877,
              "label": "Continuous integration",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "SOFT",
              "count": 0
            },
            {
              "id": 38495,
              "label": "Deployments",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "SOFT",
              "count": 0
            },
            {
              "id": 41023,
              "label": "Service delivery",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 38534,
              "label": "Optimization",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "SOFT",
              "count": 0
            },
            {
              "id": 40843,
              "label": "Communication systems",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 26898,
              "label": "Project management",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "SOFT",
              "count": 0
            },
            {
              "id": 41138,
              "label": "Storage engineering",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 31219,
              "label": "Decision making",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "SOFT",
              "count": 0
            },
            {
              "id": 31041,
              "label": "DevOps",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 26428,
              "label": "Database administration",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 33469,
              "label": "Security engineering",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 40706,
              "label": "Security",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 36216,
              "label": "Financial engineering",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 34399,
              "label": "Software development",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 37389,
              "label": "Programming",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 18409,
              "label": "Application management",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "SOFT",
              "count": 0
            },
            {
              "id": 39726,
              "label": "Project management office",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 42440,
              "label": "Help desk",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 42441,
              "label": "Hosting",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 37410,
              "label": "Databases",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 26477,
              "label": "Search Engine Optimization",
              "translation": "",
              "acronym": "SEO",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 26991,
              "label": "System administration",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 44180,
              "label": "Design",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "SOFT",
              "count": 0
            },
            {
              "id": 40890,
              "label": "Hardware",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 38451,
              "label": "Architecture",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 44177,
              "label": "Dedication",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "SOFT",
              "count": 0
            },
            {
              "id": 40135,
              "label": "Telecommunications engineering",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 43917,
              "label": "Presales",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 44294,
              "label": "Organizational change",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "SOFT",
              "count": 0
            },
            {
              "id": 39941,
              "label": "E-commerce",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 33602,
              "label": "Internet marketing",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "SOFT",
              "count": 0
            },
            {
              "id": 18792,
              "label": "Network management",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 31134,
              "label": "Adaptability",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "SOFT",
              "count": 0
            },
            {
              "id": 34489,
              "label": "Middleware",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 31098,
              "label": "Organizational skills",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "SOFT",
              "count": 0
            },
            {
              "id": 30962,
              "label": "Technical documentation",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 40871,
              "label": "Systems management",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 40757,
              "label": "Integrations",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 31001,
              "label": "Leadership",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "SOFT",
              "count": 0
            },
            {
              "id": 37622,
              "label": "Software architecture",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 43580,
              "label": "Supply chain",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            }
          ]
        },
        "seniority": {
          "id": 1,
          "label": "Junior (0-2 yrs)"
        },
        "employer": {
          "id": "27642083",
          "name": "TOMORROW JOBS",
          "superaliasid": "27642083",
          "superalias": "TOMORROW JOBS",
          "translation": "TOMORROW JOBS"
        },
        "locations": {
          "location": [
            {
              "city": {
                "id": 3080771,
                "code": "",
                "label": "Nancy",
                "translation": "Nancy"
              },
              "state": {
                "id": 4056,
                "code": "",
                "acronym": null,
                "label": "Meurthe-et-Moselle",
                "translation": "Meurthe-et-Moselle"
              },
              "county": {
                "id": 0,
                "code": "",
                "label": "Unavailable"
              },
              "wib": {
                "id": 0,
                "code": "00000",
                "label": "Unavailable"
              },
              "msa": {
                "id": 4381,
                "code": "",
                "label": "Nancy",
                "translation": "Nancy"
              },
              "superregion": {
                "id": 0,
                "code": "00",
                "label": "Unavailable"
              },
              "country": {
                "id": 80,
                "code": "FR",
                "label": "France"
              },
              "position": {
                "latitude": "48.692100524902344",
                "longitude": "6.187759876251221"
              }
            }
          ]
        },
        "salaries": {
          "salary": [
            {
              "id": 2,
              "type": "Modeled",
              "value": 77000
            },
            {
              "id": 3,
              "type": "FR",
              "value": 131600
            }
          ]
        },
        "sources": {
          "source": [
            {
              "id": 113426,
              "jobid": "4242870269",
              "tags": "",
              "type": "6",
              "name": "jobintree",
              "url": "https://bbb",
              "validlink": true
            }
          ]
        },
        "occupation": {
          "occupation": {
            "id": 10011,
            "code": "11302100",
            "label": "Computer and Information Systems Managers",
            "revision": "2011",
            "option": null
          }
        }
      },
      {
        "dates": {
          "firstseen": "1991-01-08",
          "lastseen": "1991-02-23"
        },
        "title": {
          "value": "ALPHA (ALPHA)",
          "translation": "ALPHA (ALPHA)",
          "titleid": 6083482,
          "cleantitleid": 12578212,
          "semicleantitleid": 78683
        },
        "description": {
          "value": "last one is english",
          "translation": null
        },
        "requirements": {
          "value": "",
          "translation": null
        },
        "education": {
          "id": 5,
          "label": "Bachelor's degree"
        },
        "jobTypes": {
          "jobType": [
            {
              "id": "4",
              "label": "Full-Time"
            },
            {
              "id": "1",
              "label": "Permanent"
            }
          ]
        },
        "skillslist": {
          "element": [
            {
              "id": 34346,
              "label": "Research",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "SOFT",
              "count": 0
            },
            {
              "id": 31057,
              "label": "Enterprise architecture",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 37542,
              "label": "Computer science",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 34399,
              "label": "Software development",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "TECH",
              "count": 0
            },
            {
              "id": 38566,
              "label": "Validation",
              "translation": "",
              "acronym": "",
              "code": "",
              "type": "SOFT",
              "count": 0
            }
          ]
        },
        "seniority": {
          "id": 3,
          "label": "Senior (8+ yrs)"
        },
        "employer": {
          "id": "24832370",
          "name": "Medisyskart.com",
          "superaliasid": "24832370",
          "superalias": "Medisyskart.com",
          "translation": "Medisyskart.com"
        },
        "locations": {
          "location": [
            {
              "city": {
                "id": 3198431,
                "code": "",
                "label": "Chennai",
                "translation": "Chennai"
              },
              "state": {
                "id": 1675,
                "code": "",
                "acronym": null,
                "label": "Tamil Nādu",
                "translation": "Tamil Nādu"
              },
              "county": {
                "id": 23810,
                "code": "",
                "label": "Chennai"
              },
              "wib": {
                "id": 0,
                "code": "00000",
                "label": "Unavailable"
              },
              "msa": {
                "id": 6154,
                "code": "",
                "label": "Chennai",
                "translation": "Chennai"
              },
              "superregion": {
                "id": 0,
                "code": "00",
                "label": "Unavailable"
              },
              "country": {
                "id": 107,
                "code": "IN",
                "label": "India"
              },
              "position": {
                "latitude": "13.084600448608398",
                "longitude": "80.24839782714844"
              }
            }
          ]
        },
        "salaries": {
          "salary": [
            {
              "id": 2,
              "type": "Modeled",
              "value": 1469000
            },
            {
              "id": 3,
              "type": "IN",
              "value": 131600
            }
          ]
        },
        "sources": {
          "source": [
            {
              "id": 113727,
              "jobid": "4175725226",
              "tags": "",
              "type": "5",
              "name": "Shine",
              "url": "sss/",
              "validlink": true
            }
          ]
        },
        "occupation": {
          "occupation": {
            "id": 10011,
            "code": "11302100",
            "label": "Computer and Information Systems Managers",
            "revision": "2011",
            "option": null
          }
        }
      }
    ]
  },
  "IgnoreLocalCache": true
}

“”“

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-26 23:08:52

根据我对你项目需求的理解,你不需要两本字典。只有一个,你就可以像这样循环:

代码语言:javascript
复制
newDictionary=json.loads(str(url))
x = newDictionary['data']['job']
#for every item in ['data']['job']
for job in x:
    # print the translation if ['description']['translation'] doesn't equal None
    if job['description']['translation'] != None:
        print (job['description']['translation'])
    #else print the value in job['description']
    else:
        print(job['description']['value'])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60416328

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档