Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法从json对象访问数据_Json_R_Dataframe_Jsonlite_Rjson - Fatal编程技术网

无法从json对象访问数据

无法从json对象访问数据,json,r,dataframe,jsonlite,rjson,Json,R,Dataframe,Jsonlite,Rjson,我将json数据结构存储在一个数据框中,该数据框中有一列,名为json\u datain R 所以我的json文本是以这种格式存储的 row 1) { "results" : [ { "formatted_address" : "Sahibzada Ajit Singh Nagar, Punjab, India", "types" : [ "route" ], "location" :"lat"31.1471305,"lng" 75.341217

我将json数据结构存储在一个数据框中,该数据框中有一列,名为json\u datain R 所以我的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 from location进行访问,并将它们保存到三个不同的列中,以便

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是数据帧的名称

我正在努力

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,我想从该行访问第一个出现的“格式化的_地址”及其位置下的“lat”、“lng”。但这并没有给我结果,也没有给我任何关于应该做什么改变的建议。提前谢谢

编辑:

dput(头部(json_数据_df,1))

O\p


这里有一种可能性:

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)
库(rjson)

json_data这里有一种可能性:

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)
库(rjson)

json_data好的,在澄清之后,您拥有的是data.frame的每一行的json字符串

为了重新创建数据,我将获取您提供的单个JSON字符串(行),并将其放在data.frame的两行中

因此,要访问数据,需要分别解析data.frame的每一行

但是,在您的问题中,您要求“位置下的
格式化的\u地址的第一次出现”。
在您给出的数据行中,有七个不同的位置,您可以通过
place\u id
值的数量看到,每个
place\u id
只有一个
格式化的\u地址。
因此,要求第一次出现是没有意义的,因为只有一次

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

好的,在澄清之后,您得到的是data.frame的每一行的JSON字符串

为了重新创建数据,我将获取您提供的单个JSON字符串(行),并将其放在data.frame的两行中

因此,要访问数据,需要分别解析data.frame的每一行

但是,在您的问题中,您要求“位置下的
格式化的\u地址的第一次出现”。
在您给出的数据行中,有七个不同的位置,您可以通过
place\u id
值的数量看到,每个
place\u id
只有一个
格式化的\u地址。
因此,要求第一次出现是没有意义的,因为只有一次

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


您可以使用dput()共享一些数据吗。在json文件中读/写时,使用
simplifyVector=TRUE
获取可读数据。frame@timfaber我只复制了dataframe的一条记录。因为正如你所看到的,相当大的JSONO澄清:你的每一行数据都有一个JSON对象。frame?是的,我的每一行数据都有JSON对象。你能共享一些数据吗使用dput()。在json文件中读/写时,使用
simplifyVector=TRUE
获取可读数据。frame@timfaber只复制了dataframe的一条记录..因为正如您所看到的,相当大的JSONO澄清:您的每一行数据都有一个JSON对象。frame?是的,我的每一行dataframe都有一个JSON对象您的代码得到执行,但是输出对象没有任何内容,它显示“0的列表”。。为什么要将路径添加到json_文件中
json\u文件是的,我已经将路径添加到了我的.json文件中。是否
lappy(json\u data$results,function(x)x)
给出了任何结果?我将您的
dput(head(json_data_df,1))
输出复制到一个文件中,因此您的实际总体结构可能还有另一个级别?不,这仍然会给我“0的列表”,您的代码得到执行,但输出对象没有任何内容,它显示“0的列表”。。为什么要将路径添加到json_文件中
json\u文件是的,我已经将路径添加到了我的.json文件中。是否
lappy(json\u data$results,function(x)x)
给出了任何结果?我将您的
dput(head(json_data_df,1))
输出复制到一个文件中,因此您的实际总体结构可能还有另一个级别?不,这仍然会给我“0列表”。您的第一种方法(将json数据读取为数据帧)只适用于一条记录,如果我在整个数据集上运行它,则会给我错误“Error:parse Error:trailing garbage}],“status”:“OK”}{“results”:[{(在这里)----^“下一件事我要求你再次阅读我的文章,因为我想要的是第一次出现的“格式化的_地址”及其“lat”和“lng”的记录“。最后一件事是,如果我使用您的下一种方法,即通过.json文件读取数据,执行此操作---->json_数据$results$formatted_address..给出错误::json_数据中的错误$results:$运算符对原子向量无效::类(json_数据)=”字符”1.听起来你的JSON格式不正确。2.你的问题中没有提到“首次出现”——也许你需要编辑它来澄清你的要求?3.根据你提供的数据,这些方法是有效的。要准确地看到你的错误,你需要生成一个示例数据集来演示你的错误。最后,数据来自from Google Places API?重点是所有这七个不同的位置都属于数据帧的一行;第二行数据帧由其他七个不同的位置组成。这就是为什么我说第一个出现“格式化地址”的原因。正如您看到的示例行2)它包含七个不同的“格式化地址”,我想访问它的第一个匹配项以及几何-->位置下的“lat”和“lng”。您的第一种方法(将json数据读取为dataframe)只适用于一条记录,如果我在整个数据集上运行它,它会给我错误“error:parse error:trailing garbage}],“status”:“OK”}{“results”:[{(就在这里)----“下一件事我要求你再看一次我的帖子,因为我想要的是第一次出现的“格式化地址”及其“lat”和“lng”的记录。如果我去的话,最后一件事是