如何在R中建立这个JSON档案?

问题描述

在R中工作时,我很难构建要在API调用中使用的JSON文件

所需的JSON文件格式可以在此处查看:

Shipper_Latitude <- c(1,2,3,4)
Shipper_Longitude <- c(1,4)
r_combine.NewShipperLat <- c(1,4)
r_combine.NewShipperLon <- c(1,4)
r4 <- data.frame(Shipper_Latitude,Shipper_Longitude,r_combine.NewShipperLat,r_combine.NewShipperLon)

该练习的输入是一个像这样的数据框:

# assemble lat and long for starting location:
tempfuna <- function(Lat,Lon) {list(Coords = list(Lat = Lat,Lon = Lon))}
df_jsona <- mapply(FUN = tempfuna,Lat = r4$Shipper_Latitude,Lon = r4$Shipper_Longitude)
df_jsona <- lapply(df_jsona,function(x) {list(Coords = x)})

# assemble lat and long for ending location:
tempfunb <- function(Lat,Lon = Lon))}
df_jsonb <- mapply(FUN = tempfunb,Lat = r4$r_combine.NewShipperLat,Lon = r4$r_combine.NewShipperLon)
df_jsonb <- lapply(df_jsonb,function(x) {list(Coords = x)})

# assemble list of ReportRoutes:
tempfunc <- function(A,B) {list(ReportRoutes = list(Stops = list(A,B)))}
df_jsonc <- mapply(FUN = tempfunc,A = df_jsona,B = df_jsonb)

# create final list:
post_body <- list(ReportRoutes = df_jsonc)

我尝试构建所需的JSON文件如下:

public boolean isDate(String format)
{   
  int year = format.subString(0,3); //You must validate that the subString is Integer
  int month = format.subString(5,6); //Same
  int day = format.subString(8,9); //Same


  if(month <= 12 && month >= 1)
  {
     //is valid
     if(day <= 31 && day >= 1)
     {
        if(String.valueOf(format.chatAt(4)).equals("-") && String.valueOf(format.chatAt(7)).equals("-")
        return true;
     }
  }
  .
 }

在API调用中使用生成文件时出现错误

我认为问题在于ReportRoutes列表中的列表项名称不正确。例如,第一个项目的名称为“ Coords.ReportRoutes”,而不是[[1]]。

如何重做以上内容生成所需的JSON文件

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)