Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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
如何从R编写一个包含lat和long序列的json数组?_Json_R - Fatal编程技术网

如何从R编写一个包含lat和long序列的json数组?

如何从R编写一个包含lat和long序列的json数组?,json,r,Json,R,如何从R编写一个包含lat和long序列的json数组 我想写: [[[1,2],[3,4],[5,6]]] 我所能做的就是: toJSON(matrix(1:6, ncol = 2, byrow = T)) #"[ [ 1, 2 ],\n[ 3, 4 ],\n[ 5, 6 ] ]" 我如何将它包装到另一个数组(json类型)中? 这对我来说很重要,因此我可以将文件作为行字符串写入geojson格式。如果您有矩阵 m1 <- matrix(1:6, ncol=2, byrow=T)

如何从R编写一个包含lat和long序列的json数组

我想写:

[[[1,2],[3,4],[5,6]]]
我所能做的就是:

toJSON(matrix(1:6, ncol = 2, byrow = T))
#"[ [ 1, 2 ],\n[ 3, 4 ],\n[ 5, 6 ] ]"
我如何将它包装到另一个数组(json类型)中?
这对我来说很重要,因此我可以将文件作为行字符串写入geojson格式。

如果您有
矩阵

 m1 <- matrix(1:6, ncol=2, byrow=T)

我通常使用
fromJSON
获取目标对象:

ll <- fromJSON('[[[1,2],[3,4],[5,6]]]')

str(ll)
List of 1
 $ :List of 3
  ..$ : num [1:2] 1 2
  ..$ : num [1:2] 3 4
  ..$ : num [1:2] 5 6

ll您能用
LineString
显示最终格式吗?如果您不知道,
rgdal
包可以写出geojson。例如:
库(rgdal);图书馆(sp);xy非常聪明!我会记住的,谢谢。我很惭愧我没有想到这一点。
ll <- fromJSON('[[[1,2],[3,4],[5,6]]]')

str(ll)
List of 1
 $ :List of 3
  ..$ : num [1:2] 1 2
  ..$ : num [1:2] 3 4
  ..$ : num [1:2] 5 6
 xx <- list(setNames(split(1:6,rep(1:3,each=2)),NULL))
identical(toJSON(xx),'[[[1,2],[3,4],[5,6]]]')
[1] TRUE