使用python汇总json对象

时间:2017-03-22 22:59:38

标签: python json

我在总结json对象的实例时遇到了问题。 这是我的json

avcodec_open2

我遇到的问题是使用python

获取此json文件中的成员数

这是我的python代码

data = {
      "info": {
        "name": "TOM", 
        "tags": [], 
        "beneficiaryType": "Resident", 
        "area": "Hapakule / Western", 
        "token": "a888ce4e-0c1a-4570-9a0e-9e6a28b22695", 
        "location": "location:f008b306-c980-4ef6-840a-f15e7c44d2ed", 
        "address": "Dunga", 
        "additionalFields": {
          "gpsLat": "02.45914", 
          "gpsLong": "033.70971"
        }
      } 

      }, 
      "members": [
        {
          "info": {
            "documentType": "NO ID Tag", 
            "firstName": "BEN", 
            "tags": [], 
            "middleName": "JAMBI", 
            "gender": "Male", 
            "physicalDisability": 0, 
            "beneficiary": false, 
            "dateOfBirth": "2003-03-02T11:00:13.021Z", 
            "documentNumber": "BEN10", 
            "dateOfBirthExact": false, 
            "role": "Child", 
            "lastName": "TOM", 
            "recipient": false, 
            "mentalDisability": 0, 
            "additionalFields": {
              "beneficiaryCategory": "Youth" 

            }
          }, 
          "meta": {
            "geolocation": {
              "message": "The pipe is being closed.\r\n", 
              "code": 2
            }, 
            "created": "2017-03-02T11:00:38.940Z"
          }, 
          "id": "f3ba5dc6-69ae-4ed1-88aa-17351316eb09"
        }, 
        {
          "info": {
            "documentType": "NO ID Tag", 
            "firstName": "BOSCO", 
            "tags": [], 
            "middleName": "LOCHORO", 
            "gender": "Male", 
            "physicalDisability": 0, 
            "beneficiary": false, 
            "dateOfBirth": "2007-03-02T11:01:13.107Z", 
            "documentNumber": "BOSCOLOCHORO", 
            "dateOfBirthExact": false, 
            "role": "Child", 
            "lastName": "MARIKO", 
            "recipient": false, 
            "mentalDisability": 0, 
            "additionalFields": {
              "beneficiaryCategory": "Child", 

            }
          }, 
          "meta": {
            "geolocation": {
              "message": "The pipe is being closed.\r\n", 
              "code": 2
            }, 
            "created": "2017-03-02T11:01:43.855Z"
          }, 
          "id": "c95fe7c0-61dd-469d-9976-027193a96b80"
        }, 
        {
          "info": {
            "documentType": "NO ID Tag", 
            "firstName": "PAULINA", 
            "tags": [], 
            "middleName": "LOTUKEI", 
            "gender": "Female", 
            "physicalDisability": 0, 
            "beneficiary": false, 
            "dateOfBirth": "1989-03-02T10:58:31.182Z", 
            "documentNumber": "PAULINALOTUKEI", 
            "dateOfBirthExact": false, 
            "role": "Spouse", 
            "lastName": "MARIKO", 
            "recipient": false, 
            "mentalDisability": 0, 
            "additionalFields": {
              "beneficiaryCategory": "Youth"

            }
          }, 
          "meta": {
            "geolocation": {
              "message": "The pipe is being closed.\r\n", 
              "code": 2
            }, 
            "created": "2017-03-02T10:58:58.486Z"
          }, 
          "id": "d898934a-0402-47f5-bcc3-4750fe52bec7"
        }, 
        {
          "info": {
            "documentType": "National ID", 
            "firstName": "PETER", 
            "tags": [], 
            "gender": "Male", 
            "physicalDisability": 0, 
            "beneficiary": false, 
            "dateOfBirth": "1979-02-03T00:00:00.000Z", 
            "documentNumber": "CM79107100RQ5H", 
            "dateOfBirthExact": true, 
            "role": "Head", 
            "lastName": "MARIKO", 
            "recipient": false, 
            "mentalDisability": 0, 
            "additionalFields": {
              "beneficiaryCategory": "Unskilled "

            }
          }, 
          "photo": "mb-04d31c8a-1b91-433f-bbb6-d09de874946d-photo.jpg", 
          "meta": {
            "geolocation": {
              "message": "The pipe is being closed.\r\n", 
              "code": 2
            }, 
            "created": "2017-03-02T10:57:56.482Z"
          }, 

          }
        }
      ], 
      "_id": "hh:a888ce4e-0c1a-4570-9a0e-9e6a28b22695"
    }
   },
   "info": {
        "name": "TOM", 
        "tags": [], 
        "beneficiaryType": "Resident", 
        "area": "Hapakule / Western", 
        "token": "a888ce4e-0c1a-4570-9a0e-9e6a28b22695", 
        "location": "location:f008b306-c980-4ef6-840a-f15e7c44d2ed", 
        "address": "Dunga", 
        "additionalFields": {
          "gpsLat": "02.45914", 
          "gpsLong": "033.70971"
        }
      }, 
      "_rev": "10-56fcc968e36e563a85150626f9c7174c", 
      "meta": {
        "geolocation": {
          "message": "The pipe is being closed.\r\n", 
          "code": 2
        }, 
        "modified": "2017-03-02T11:01:43.861Z", 
        "created": "2017-03-02T10:56:59.947Z"
      }, 
      "members": [
        {
          "info": {
            "documentType": "NO ID Tag", 
            "firstName": "BEN", 
            "tags": [], 
            "middleName": "JAMBI", 
            "gender": "Male", 
            "physicalDisability": 0, 
            "beneficiary": false, 
            "dateOfBirth": "2003-03-02T11:00:13.021Z", 
            "documentNumber": "BEN10", 
            "dateOfBirthExact": false, 
            "role": "Child", 
            "lastName": "TOM", 
            "recipient": false, 
            "mentalDisability": 0, 
            "additionalFields": {
              "beneficiaryCategory": "Youth" 

            }
          }, 
          "meta": {
            "geolocation": {
              "message": "The pipe is being closed.\r\n", 
              "code": 2
            }, 
            "created": "2017-03-02T11:00:38.940Z"
          }, 
          "id": "f3ba5dc6-69ae-4ed1-88aa-17351316eb09"
        }, 
        {
          "info": {
            "documentType": "NO ID Tag", 
            "firstName": "BOSCO", 
            "tags": [], 
            "middleName": "LOCHORO", 
            "gender": "Male", 
            "physicalDisability": 0, 
            "beneficiary": false, 
            "dateOfBirth": "2007-03-02T11:01:13.107Z", 
            "documentNumber": "BOSCOLOCHORO", 
            "dateOfBirthExact": false, 
            "role": "Child", 
            "lastName": "MARIKO", 
            "recipient": false, 
            "mentalDisability": 0, 
            "additionalFields": {
              "beneficiaryCategory": "Child", 

            }
          }, 
          "meta": {
            "geolocation": {
              "message": "The pipe is being closed.\r\n", 
              "code": 2
            }, 
            "created": "2017-03-02T11:01:43.855Z"
          }, 
          "id": "c95fe7c0-61dd-469d-9976-027193a96b80"
        }, 
        {
          "info": {
            "documentType": "NO ID Tag", 
            "firstName": "PAULINA", 
            "tags": [], 
            "middleName": "LOTUKEI", 
            "gender": "Female", 
            "physicalDisability": 0, 
            "beneficiary": false, 
            "dateOfBirth": "1989-03-02T10:58:31.182Z", 
            "documentNumber": "PAULINALOTUKEI", 
            "dateOfBirthExact": false, 
            "role": "Spouse", 
            "lastName": "MARIKO", 
            "recipient": false, 
            "mentalDisability": 0, 
            "additionalFields": {
              "beneficiaryCategory": "Youth"

            }
          }, 
          "meta": {
            "geolocation": {
              "message": "The pipe is being closed.\r\n", 
              "code": 2
            }, 
            "created": "2017-03-02T10:58:58.486Z"
          }, 
          "id": "d898934a-0402-47f5-bcc3-4750fe52bec7"
        }, 
        {
          "info": {
            "documentType": "National ID", 
            "firstName": "PETER", 
            "tags": [], 
            "gender": "Male", 
            "physicalDisability": 0, 
            "beneficiary": false, 
            "dateOfBirth": "1979-02-03T00:00:00.000Z", 
            "documentNumber": "CM79107100RQ5H", 
            "dateOfBirthExact": true, 
            "role": "Head", 
            "lastName": "MARIKO", 
            "recipient": false, 
            "mentalDisability": 0, 
            "additionalFields": {
              "beneficiaryCategory": "Unskilled "

            }
          }, 
          "photo": "mb-04d31c8a-1b91-433f-bbb6-d09de874946d-photo.jpg", 
          "meta": {
            "geolocation": {
              "message": "The pipe is being closed.\r\n", 
              "code": 2
            }, 
            "created": "2017-03-02T10:57:56.482Z"
          }, 

          }
        }
      ], 
      "_id": "hh:a888ce4e-0c1a-4570-9a0e-9e6a28b22695"
    }

问题是这个家庭的成员总数没有得到,我怎么能解决这个问题呢?

1 个答案:

答案 0 :(得分:1)

您可以使用len(data["members"]),因为它是一个列表。

print "name is %s" % data["info"]["name"]
print "total number of members is %s" % str(len(data["info"]["members"]))