sourcetip

JSON 데이터의 항목 카운트 방법

fileupload 2023. 2. 17. 21:34
반응형

JSON 데이터의 항목 카운트 방법

JSON 데이터 노드의 요소 수를 얻는 방법은 무엇입니까?

JSON:

{
  "result":[
    {
      "run":[
        {
          "action":"stop"
        },
        {
          "action":"start"
        },
        {
          "action":"start"
        }
      ],
      "find":true
    }
  ]
}

노드로부터 요소의 수를 취득할 필요가 있다.data['result'][0]['run']3이 되어야 하는데 Python에서 하는 방법을 찾을 수가 없어요.

import json

json_data = json.dumps({
  "result":[
    {
      "run":[
        {
          "action":"stop"
        },
        {
          "action":"start"
        },
        {
          "action":"start"
        }
      ],
      "find": "true"
    }
  ]
})

item_dict = json.loads(json_data)
print len(item_dict['result'][0]['run'])

받아쓰기로 변환합니다.

거의 다 왔어.정말 간단한 해결책은 반환된 '실행' 객체로부터 길이를 얻는 것입니다.'부하'나 '부하'에 신경 쓸 필요 없음:

len(data['result'][0]['run'])

같은 작업을 하고 있었습니다만, JSON 파일로부터 데이터를 읽어낼 필요가 있었습니다.그 방법은 다음과 같습니다.

import json
with open('movie.json', encoding='utf8') as JSONFile:
    data = json.load(JSONFile)
print(len(data['movie'][0]))

파일 내의 샘플 JSON 요소는 다음과 같습니다.

{
"movie": [
    {
        "Id": 1,
        "Title": "Inception",
        "Overview": "Cobb, a skilled thief who commits corporate espionage by infiltrating the subconscious of his targets is offered a chance to regain his old life as payment for a task considered to be impossible: \"inception\", the implantation of another person's idea into a target's subconscious.",
        "Tagline": "Your mind is the scene of the crime.",
        "Budget": 160000000.0000,
        "Revenue": 825532764.0000,
        "ImdbUrl": "https:\/\/www.imdb.com\/title\/tt1375666",
        "TmdbUrl": "https:\/\/www.themoviedb.org\/movie\/27205",
        "PosterUrl": "https:\/\/image.tmdb.org\/t\/p\/w342\/\/9gk7adHYeDvHkCSEqAvQNLV5Uge.jpg",
        "BackdropUrl": "https:\/\/image.tmdb.org\/t\/p\/original\/\/s3TBrRGB1iav7gFOCNx3H31MoES.jpg",
        "OriginalLanguage": "en",
        "ReleaseDate": "2010-07-15T00:00:00",
        "RunTime": 148,
        "Price": 9.90,
        "CreatedDate": "2021-04-03T16:51:30.1633333",
        "UpdatedDate": null,
        "UpdatedBy": null,
        "CreatedBy": null,
        "genres": [
            {
                "id": 1,
                "name": "Adventure"
            },
            {
                "id": 6,
                "name": "Action"
            },
            {
                "id": 13,
                "name": "Science Fiction"
            }
        ]
    }]

언급URL : https://stackoverflow.com/questions/27315472/how-to-count-items-in-json-data

반응형