无法从json对象访问数据

时间:2017-06-01 12:02:26

标签: json r dataframe jsonlite rjson

我已将json数据结构存储在具有单列的数据框中,在R中名为 json_data 所以我的json文本以这种格式存储

row 1)    { "results" : [ { "formatted_address" : "Sahibzada Ajit Singh Nagar, 
          Punjab, India", "types" : [ "route" ], "location" :"lat"31.1471305,"lng" 
          75.34121789999999 }, ]
row 2)     { "results" : [ { "formatted_address" : "SAS Nagar,  Chennai, India", 
       "types" : [ "route 2" ], "location" :"lat"30.67249,"lng" 23.988672537 ,}] 
row 3)   { "results" : [ { "formatted_address" : "Hind mata chowk, UP, India", 
       "types" : [ "route 4" ], "location" lat"48.973478983,"lng"64.982486763},] 
row 4)
row 5)
.
.
.
.row 1000)

我想要实现的是想要从每一行格式化地址,lat,lng从位置访问并将它们保存到三个不同的列中,以便

formatted_add                                       lat            lng
Sahibzada Ajit Singh Nagar,Punjab, India      31.1471305       75.34121789999999
SAS Nagar,  Chennai, India                    30.67249         23.988672537
Hind mata chowk, UP, India                    48.973478983     64.982486763

json_data_df是dataframe的名称

我正在尝试

formatted_add=json_data_df$json_data$formatted_address
lat=json_data_df$json_data$formatted_address$geometry$location$lat
lng=json_data_df$json_data$formatted_address$geometry$location$lng

请查看我已粘贴样本行的dput()的O / P,从该行我想要访问第一次出现的&#34; formatted_address&#34; 及其<在位置下强>&#34; lat&#34;&#34; lng&#34; 。但这并没有给我输出,有关应该做出哪些改变的任何建议。提前致谢

编辑:

dput(头(json_data_df,1))

0 \ P

    {
        "results": [{
            "address_components": [{
                "long_name": "Unnamed Road",
                "short_name": "Unnamed Road",
                "types": ["route"]
            }, {
                "long_name": "Sahibzada Ajit Singh Nagar",
                "short_name": "SAS Nagar",
                "types": ["administrative_area_level_2", "political"]
            }, {
                "long_name": "Punjab",
                "short_name": "PB",
                "types": ["administrative_area_level_1", "political"]
            }, {
                "long_name": "India",
                "short_name": "IN",
                "types": ["country", "political"]
            }, {
                "long_name": "160003",
                "short_name": "160003",
                "types": ["postal_code"]
            }],
            "formatted_address": "Unnamed Road, Punjab 160003, India",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 30.6593213,
                        "lng": 76.7952105
                    },
                    "southwest": {
                        "lat": 30.6548837,
                        "lng": 76.7931289
                    }
                },
                "location": {
                    "lat": 30.6576094,
                    "lng": 76.794392
                },
                "location_type": "GEOMETRIC_CENTER",
                "viewport": {
                    "northeast": {
                        "lat": 30.6593213,
                        "lng": 76.79551868029151
                    },
                    "southwest": {
                        "lat": 30.6548837,
                        "lng": 76.7928207197085
                    }
                }
            },
            "place_id": "ChIJYWQws23rDzkRsqz31TNCwXg",
            "types": ["route"]
        }, {
            "address_components": [{
                "long_name": "Bhabat",
                "short_name": "Bhabat",
                "types": ["locality", "political"]
            }, {
                "long_name": "Sahibzada Ajit Singh Nagar",
                "short_name": "SAS Nagar",
                "types": ["administrative_area_level_2", "political"]
            }, {
                "long_name": "Punjab",
                "short_name": "PB",
                "types": ["administrative_area_level_1", "political"]
            }, {
                "long_name": "India",
                "short_name": "IN",
                "types": ["country", "political"]
            }],
            "formatted_address": "Bhabat, Punjab, India",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 30.67249,
                        "lng": 76.81895999999999
                    },
                    "southwest": {
                        "lat": 30.64508,
                        "lng": 76.79106999999999
                    }
                },
                "location": {
                    "lat": 30.6575248,
                    "lng": 76.8057776
                },
                "location_type": "APPROXIMATE",
                "viewport": {
                    "northeast": {
                        "lat": 30.67249,
                        "lng": 76.81895999999999
                    },
                    "southwest": {
                        "lat": 30.64508,
                        "lng": 76.79106999999999
                    }
                }
            },
            "place_id": "ChIJN3sGJEHrDzkRwGZ1GMXBa2Q",
            "types": ["locality", "political"]
        }, {
            "address_components": [{
                "long_name": "Chandigarh Airport Area",
                "short_name": "Chandigarh Airport Area",
                "types": ["political", "sublocality", "sublocality_level_1"]
            }, {
                "long_name": "Chandigarh",
                "short_name": "Chandigarh",
                "types": ["locality", "political"]
            }, {
                "long_name": "Chandigarh",
                "short_name": "CH",
                "types": ["administrative_area_level_1", "political"]
            }, {
                "long_name": "India",
                "short_name": "IN",
                "types": ["country", "political"]
            }],
            "formatted_address": "Chandigarh Airport Area, Chandigarh, India",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 30.696639,
                        "lng": 76.81389089999999
                    },
                    "southwest": {
                        "lat": 30.6568821,
                        "lng": 76.76542999999999
                    }
                },
                "location": {
                    "lat": 30.6783321,
                    "lng": 76.7884713
                },
                "location_type": "APPROXIMATE",
                "viewport": {
                    "northeast": {
                        "lat": 30.696639,
                        "lng": 76.81389089999999
                    },
                    "southwest": {
                        "lat": 30.6568821,
                        "lng": 76.76542999999999
                    }
                }
            },
            "place_id": "ChIJcb4-lILsDzkRJIQJTpsMm8w",
            "types": ["political", "sublocality", "sublocality_level_1"]
        }, {
            "address_components": [{
                "long_name": "160004",
                "short_name": "160004",
                "types": ["postal_code"]
            }, {
                "long_name": "India",
                "short_name": "IN",
                "types": ["country", "political"]
            }],
            "formatted_address": "160004, India",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 30.6832516,
                        "lng": 76.8121771
                    },
                    "southwest": {
                        "lat": 30.6447985,
                        "lng": 76.7639648
                    }
                },
                "location": {
                    "lat": 30.6582706,
                    "lng": 76.7855866
                },
                "location_type": "APPROXIMATE",
                "viewport": {
                    "northeast": {
                        "lat": 30.6832516,
                        "lng": 76.8121771
                    },
                    "southwest": {
                        "lat": 30.6447985,
                        "lng": 76.7639648
                    }
                }
            },
            "place_id": "ChIJf560w6fsDzkRXLKn0s52kHM",
            "types": ["postal_code"]
        }, {
            "address_components": [{
                "long_name": "Sahibzada Ajit Singh Nagar",
                "short_name": "SAS Nagar",
                "types": ["administrative_area_level_2", "political"]
            }, {
                "long_name": "Punjab",
                "short_name": "PB",
                "types": ["administrative_area_level_1", "political"]
            }, {
                "long_name": "India",
                "short_name": "IN",
                "types": ["country", "political"]
            }],
            "formatted_address": "Sahibzada Ajit Singh Nagar, Punjab, India",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 30.9369199,
                        "lng": 76.9439199
                    },
                    "southwest": {
                        "lat": 30.35902,
                        "lng": 76.52444009999999
                    }
                },
                "location": {
                    "lat": 30.6496486,
                    "lng": 76.7567368
                },
                "location_type": "APPROXIMATE",
                "viewport": {
                    "northeast": {
                        "lat": 30.9369199,
                        "lng": 76.9439199
                    },
                    "southwest": {
                        "lat": 30.35902,
                        "lng": 76.52444009999999
                    }
                }
            },
            "place_id": "ChIJOyhuuFPlDzkRzrRj0bvdZPw",
            "types": ["administrative_area_level_2", "political"]
        }, {
            "address_components": [{
                "long_name": "Punjab",
                "short_name": "PB",
                "types": ["administrative_area_level_1", "political"]
            }, {
                "long_name": "India",
                "short_name": "IN",
                "types": ["country", "political"]
            }],
            "formatted_address": "Punjab, India",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 32.4994008,
                        "lng": 76.92175809999999
                    },
                    "southwest": {
                        "lat": 29.537147,
                        "lng": 73.88057999999999
                    }
                },
                "location": {
                    "lat": 31.1471305,
                    "lng": 75.34121789999999
                },
                "location_type": "APPROXIMATE",
                "viewport": {
                    "northeast": {
                        "lat": 32.4994008,
                        "lng": 76.92175809999999
                    },
                    "southwest": {
                        "lat": 29.537147,
                        "lng": 73.88057999999999
                    }
                }
            },
            "place_id": "ChIJVXOeVqpkGTkRYYijAzEmvY8",
            "types": ["administrative_area_level_1", "political"]
        }, {
            "address_components": [{
                "long_name": "India",
                "short_name": "IN",
                "types": ["country", "political"]
            }],
            "formatted_address": "India",
            "geometry": {
                "bounds": {
                    "northeast": {
                        "lat": 35.5087008,
                        "lng": 97.39535869999999
                    },
                    "southwest": {
                        "lat": 6.4626999,
                        "lng": 68.1097
                    }
                },
                "location": {
                    "lat": 20.593684,
                    "lng": 78.96288
                },
                "location_type": "APPROXIMATE",
                "viewport": {
                    "northeast": {
                        "lat": 35.5087008,
                        "lng": 97.39498069999999
                    },
                    "southwest": {
                        "lat": 6.7535159,
                        "lng": 68.16288519999999
                    }
                }
            },
            "place_id": "ChIJkbeSa_BfYzARphNChaFPjNc",
            "types": ["country", "political"]
        }],
        "status": "OK"
    }

row 2)
      {    "results" : [       {          "address_components" : [             {                "long_name" : "Unnamed Road",                "short_name" : "Unnamed Road",                "types" : [ "route" ]             },             {                "long_name" : "Godown Area",                "short_name" : "Godown Area",                "types" : [ "neighborhood", "political" ]             },             {                "long_name" : "Chandigarh Airport Area",                "short_name" : "Chandigarh Airport Area",                "types" : [ "political", "sublocality", "sublocality_level_1" ]             },             {                "long_name" : "Chandigarh",                "short_name" : "Chandigarh",                "types" : [ "locality", "political" ]             },             {                "long_name" : "Sahibzada Ajit Singh Nagar",                "short_name" : "SAS Nagar",                "types" : [ "administrative_area_level_2", "political" ]             },             {                "long_name" : "Chandigarh",                "short_name" : "CH",                "types" : [ "administrative_area_level_1", "political" ]             },             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             },             {                "long_name" : "140604",                "short_name" : "140604",                "types" : [ "postal_code" ]             }          ],          "formatted_address" : "Unnamed Road, Godown Area, Chandigarh Airport Area, Chandigarh, 140604, India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 30.6617314,                   "lng" : 76.81240459999999                },                "southwest" : {                   "lat" : 30.6611368,                   "lng" : 76.8107763                }             },             "location" : {                "lat" : 30.66141159999999,                "lng" : 76.8115756             },             "location_type" : "GEOMETRIC_CENTER",             "viewport" : {                "northeast" : {                   "lat" : 30.66278308029149,                   "lng" : 76.81293943029151                },                "southwest" : {                   "lat" : 30.66008511970849,                   "lng" : 76.81024146970849                }             }          },          "place_id" : "ChIJBf4l00XrDzkRhE9fwLK7DNw",          "types" : [ "route" ]       },       {          "address_components" : [             {                "long_name" : "Bhabat",                "short_name" : "Bhabat",                "types" : [ "locality", "political" ]             },             {                "long_name" : "Sahibzada Ajit Singh Nagar",                "short_name" : "SAS Nagar",                "types" : [ "administrative_area_level_2", "political" ]             },             {                "long_name" : "Punjab",                "short_name" : "PB",                "types" : [ "administrative_area_level_1", "political" ]             },             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             }          ],          "formatted_address" : "Bhabat, Punjab, India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 30.67249,                   "lng" : 76.81895999999999                },                "southwest" : {                   "lat" : 30.64508,                   "lng" : 76.79106999999999                }             },             "location" : {                "lat" : 30.6575248,                "lng" : 76.8057776             },             "location_type" : "APPROXIMATE",             "viewport" : {                "northeast" : {                   "lat" : 30.67249,                   "lng" : 76.81895999999999                },                "southwest" : {                   "lat" : 30.64508,                   "lng" : 76.79106999999999                }             }          },          "place_id" : "ChIJN3sGJEHrDzkRwGZ1GMXBa2Q",          "types" : [ "locality", "political" ]       },       {          "address_components" : [             {                "long_name" : "Chandigarh Airport Area",                "short_name" : "Chandigarh Airport Area",                "types" : [ "political", "sublocality", "sublocality_level_1" ]             },             {                "long_name" : "Chandigarh",                "short_name" : "Chandigarh",                "types" : [ "locality", "political" ]             },             {                "long_name" : "Chandigarh",                "short_name" : "CH",                "types" : [ "administrative_area_level_1", "political" ]             },             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             }          ],          "formatted_address" : "Chandigarh Airport Area, Chandigarh, India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 30.696639,                   "lng" : 76.81389089999999                },                "southwest" : {                   "lat" : 30.6568821,                   "lng" : 76.76542999999999                }             },             "location" : {                "lat" : 30.6783321,                "lng" : 76.7884713             },             "location_type" : "APPROXIMATE",             "viewport" : {                "northeast" : {                   "lat" : 30.696639,                   "lng" : 76.81389089999999                },                "southwest" : {                   "lat" : 30.6568821,                   "lng" : 76.76542999999999                }             }          },          "place_id" : "ChIJcb4-lILsDzkRJIQJTpsMm8w",          "types" : [ "political", "sublocality", "sublocality_level_1" ]       },       {          "address_components" : [             {                "long_name" : "140604",                "short_name" : "140604",                "types" : [ "postal_code" ]             },             {                "long_name" : "Punjab",                "short_name" : "PB",                "types" : [ "administrative_area_level_1", "political" ]             },             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             }          ],          "formatted_address" : "Punjab 140604, India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 30.6698436,                   "lng" : 76.8210886                },                "southwest" : {                   "lat" : 30.6552941,                   "lng" : 76.80184109999999                }             },             "location" : {                "lat" : 30.6625018,                "lng" : 76.8129878             },             "location_type" : "APPROXIMATE",             "viewport" : {                "northeast" : {                   "lat" : 30.6698436,                   "lng" : 76.8210886                },                "southwest" : {                   "lat" : 30.6552941,                   "lng" : 76.80184109999999                }             }          },          "place_id" : "ChIJpzrfc1DrDzkRc_qbC2Hz3tc",          "types" : [ "postal_code" ]       },       {          "address_components" : [             {                "long_name" : "Sahibzada Ajit Singh Nagar",                "short_name" : "SAS Nagar",                "types" : [ "administrative_area_level_2", "political" ]             },             {                "long_name" : "Punjab",                "short_name" : "PB",                "types" : [ "administrative_area_level_1", "political" ]             },             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             }          ],          "formatted_address" : "Sahibzada Ajit Singh Nagar, Punjab, India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 30.9369199,                   "lng" : 76.9439199                },                "southwest" : {                   "lat" : 30.35902,                   "lng" : 76.52444009999999                }             },             "location" : {                "lat" : 30.6496486,                "lng" : 76.7567368             },             "location_type" : "APPROXIMATE",             "viewport" : {                "northeast" : {                   "lat" : 30.9369199,                   "lng" : 76.9439199                },                "southwest" : {                   "lat" : 30.35902,                   "lng" : 76.52444009999999                }             }          },          "place_id" : "ChIJOyhuuFPlDzkRzrRj0bvdZPw",          "types" : [ "administrative_area_level_2", "political" ]       },       {          "address_components" : [             {                "long_name" : "Punjab",                "short_name" : "PB",                "types" : [ "administrative_area_level_1", "political" ]             },             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             }          ],          "formatted_address" : "Punjab, India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 32.4994008,                   "lng" : 76.92175809999999                },                "southwest" : {                   "lat" : 29.537147,                   "lng" : 73.88057999999999                }             },             "location" : {                "lat" : 31.1471305,                "lng" : 75.34121789999999             },             "location_type" : "APPROXIMATE",             "viewport" : {                "northeast" : {                   "lat" : 32.4994008,                   "lng" : 76.92175809999999                },                "southwest" : {                   "lat" : 29.537147,                   "lng" : 73.88057999999999                }             }          },          "place_id" : "ChIJVXOeVqpkGTkRYYijAzEmvY8",          "types" : [ "administrative_area_level_1", "political" ]       },       {          "address_components" : [             {                "long_name" : "India",                "short_name" : "IN",                "types" : [ "country", "political" ]             }          ],          "formatted_address" : "India",          "geometry" : {             "bounds" : {                "northeast" : {                   "lat" : 35.5087008,                   "lng" : 97.39535869999999                },                "southwest" : {                   "lat" : 6.4626999,                   "lng" : 68.1097                }             },             "location" : {                "lat" : 20.593684,                "lng" : 78.96288             },             "location_type" : "APPROXIMATE",             "viewport" : {                "northeast" : {                   "lat" : 35.5087008,                   "lng" : 97.39498069999999                },                "southwest" : {                   "lat" : 6.7535159,                   "lng" : 68.16288519999999                }             }          },          "place_id" : "ChIJkbeSa_BfYzARphNChaFPjNc",          "types" : [ "country", "political" ]       }    ],    "status" : "OK" }

2 个答案:

答案 0 :(得分:3)

这是一种可能性:

library(rjson)

json_data <- fromJSON(file = json_file)
formatted_add <- lapply(json_data$results, function(x) x$formatted_add)
lat <- lapply(json_data$results, function(x) x$geometry$location$lat)
lng <- lapply(json_data$results, function(x) x$geometry$location$lng)

data <- cbind(formatted_add, lat, lng)

我希望这是你需要的。

答案 1 :(得分:1)

好的,澄清之后,你拥有的是每一行data.frame的JSON字符串

要重新创建数据,我将使用您提供的单个JSON字符串(行),并将其放入data.frame的两行

因此,要访问数据,您需要单独解析data.frame的每一行

但是,在您的问题中,您要求“{strong>第一次出现 formatted_address位置”。 在您提供的数据行中,有七个不同的位置,您可以通过place_id值的数量来查看,每formatted_address只有一个place_id。 所以要求第一次出现没有意义,因为只有一个。

library(jsonlite)

js <- '{
                 "results": [{
                       "address_components": [{
                       "long_name": "Unnamed Road",
                       "short_name": "Unnamed Road",
                       "types": ["route"]
                       }, {
                       .... <i've omitted the rest of the text> ...
                       },
                       "place_id": "ChIJkbeSa_BfYzARphNChaFPjNc",
                        "types": ["country", "political"]
                       }],
                        "status": "OK"
                       }'

json_data_df <- data.frame(json_data = c(js, js))

## if parsing a row individually you would do
# json_result <- fromJSON(as.character(json_data_df[1, "json_data"]))

## as you're parsing each row of the data.frame, you can use whatever looping 
## method you like

## you can get all the data out of the JSON and create a list obect of everything
lst <- lapply(1:nrow(json_data_df), function(x){
  fromJSON(as.character(json_data_df[x, 'json_data']))
})


## or you can subset it within the loop to just get the bits you want
lst <- lapply(1:nrow(json_data_df), function(x){
  js <- fromJSON(as.character(json_data_df[x, 'json_data']))
  data.frame(
    place_id = js$results$place_id,
    formatted_address = js$results$formatted_address,
    lat = js$results$geometry$location$lat,
    lng = js$results$geometry$location$lng
    )
})

lst
# [[1]]
#                      place_id                          formatted_address      lat      lng
# 1 ChIJYWQws23rDzkRsqz31TNCwXg         Unnamed Road, Punjab 160003, India 30.65761 76.79439
# 2 ChIJN3sGJEHrDzkRwGZ1GMXBa2Q                      Bhabat, Punjab, India 30.65752 76.80578
# 3 ChIJcb4-lILsDzkRJIQJTpsMm8w Chandigarh Airport Area, Chandigarh, India 30.67833 76.78847
# 4 ChIJf560w6fsDzkRXLKn0s52kHM                              160004, India 30.65827 76.78559
# 5 ChIJOyhuuFPlDzkRzrRj0bvdZPw  Sahibzada Ajit Singh Nagar, Punjab, India 30.64965 76.75674
# 6 ChIJVXOeVqpkGTkRYYijAzEmvY8                              Punjab, India 31.14713 75.34122
# 7 ChIJkbeSa_BfYzARphNChaFPjNc                                      India 20.59368 78.96288
# 
# [[2]]
#                      place_id                          formatted_address      lat      lng
# 1 ChIJYWQws23rDzkRsqz31TNCwXg         Unnamed Road, Punjab 160003, India 30.65761 76.79439
# 2 ChIJN3sGJEHrDzkRwGZ1GMXBa2Q                      Bhabat, Punjab, India 30.65752 76.80578
# 3 ChIJcb4-lILsDzkRJIQJTpsMm8w Chandigarh Airport Area, Chandigarh, India 30.67833 76.78847
# 4 ChIJf560w6fsDzkRXLKn0s52kHM                              160004, India 30.65827 76.78559
# 5 ChIJOyhuuFPlDzkRzrRj0bvdZPw  Sahibzada Ajit Singh Nagar, Punjab, India 30.64965 76.75674
# 6 ChIJVXOeVqpkGTkRYYijAzEmvY8                              Punjab, India 31.14713 75.34122
# 7 ChIJkbeSa_BfYzARphNChaFPjNc                                      India 20.59368 78.96288

根据注释 - 只访问每个JSON对象的第一个条目,您可以只对lapply

中的第一个元素进行子集化
## or you can subset it within the loop to just get the bits you want
lst <- lapply(1:nrow(json_data_df), function(x){
  js <- fromJSON(as.character(json_data_df[x, 'json_data']))
  data.frame(
    place_id = js$results$place_id[1],
    formatted_address = js$results$formatted_address[1],
    lat = js$results$geometry$location$lat[1],
    lng = js$results$geometry$location$lng[1]
  )
})
lst

# [[1]]
#                      place_id                  formatted_address      lat      lng
# 1 ChIJYWQws23rDzkRsqz31TNCwXg Unnamed Road, Punjab 160003, India 30.65761 76.79439

# [[2]]
#                      place_id                  formatted_address      lat      lng
# 1 ChIJYWQws23rDzkRsqz31TNCwXg Unnamed Road, Punjab 160003, India 30.65761 76.79439
相关问题