我如何写一个从R有一个lat和long序列的json数组?

我如何写一个从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类)? 这对我很重要,所以我可以将文件作为LineString写入geojson格式。

author: cylondude
采纳答案:

我通常使用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

所以我们应该创建一个未命名列表,每个列表包含2个元素:

 xx <- list(setNames(split(1:6,rep(1:3,each=2)),NULL))
identical(toJSON(xx),'[[[1,2],[3,4],[5,6]]]')
[1] TRUE
author: agstudy

参考更多解答: How do I write a json array from R that has a sequence of lat and long? ,转载请保留出处我如何写一个从R有一个lat和long序列的json数组?及作者信息

Statement: We respect knowledge and authors. Since the content comes from the Internet and is intended for scientific research, any reprinters should retain the author's signature and origin. If you are the author of the content and feel in dispute, please contact email: 1076545519@qq.com. We will find out the situation and deal with it in time. We sincerely thank the author for his hard work.


更多:json