使用jsonlite或其他内容从R编码JSON表达式 [英] encoding a JSON expression from R with jsonlite or something else

查看:233
本文介绍了使用jsonlite或其他内容从R编码JSON表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的API需要我发送一个 JSON 消息,如:

  y < - '[{a:1,b:select,c:[A,B,C]},
{a:2,b:text},
{a:3,b:select,c:[D,E ,G]}]'

请注意,在 b:select还有另一个变量 c ,值为数组。



当我尝试从JSON(y)(在 jsonlite 包中)运行时,我得到一个 data.frame 其中第3列是列表:

 > z<  - 从JSON(y)
> class(z)
[1]data.frame
> z%>%glimpse()
观察:3
变量:3
$ a(int)1,2,3
$ b(chr)select文本,选择
$ c(列表)A,B,C,NULL,D,E,F,G

但是当我将其转换回 JSON (这是我要发送给API的格式) c 变量出现在不应该的位置。

  {a:1,b:select,c:[A,B,C]},
{a:2,b文本,c:{}},
{a:3,b:select,c:[D,E,F ]}]

是否有另一个R数据结构可以可靠地让我从一个数据帧到原来的 JSON 消息使用 jsonlite (或其他)?它如何工作?

解决方案

jsonlite ,你可以做如下:

  z < -  fromJSON(y,simplifiedDataFrame = F)
toJSON(z,auto_unbox = T)

另外,考虑以下情况,最后一个 c 元素是 [D]

  y2 < '{a:1,b:select,c:[A,B,C]},
{a:2,b :text},
{a:3,b:select,c:[D]}]'

z2< - fromJSON (y2,simplifiedDataFrame = F)
toJSON(z2,auto_unbox = T)

这个:

  [{a:1,b:select,c ,B,C]},
{a:2,b:text},
{a:3,b c:D}]

这可能是一个麻烦,因为最后一个 c 元素是D不是 [D] p>

为了防止这种情况,请勿使用 auto_unbox 。请仔细使用 unbox

  z2 < -  fromJSON(y2 ,simplDataFrame = F)
for(i in 1:length(z2)){
z2 [[i]] [[1]]< - unbox(z2 [[i]] [[1 ]])
z2 [[i]] [[2]]< - unbox(z2 [[i]] [[2]])
}
toJSON(z2)


The API I'm using needs me to send it a JSON message like:

y <- '[{"a":1, "b":"select", "c":["A", "B", "C"]}, 
       {"a":2, "b":"text"},
       {"a":3, "b":"select", "c":["D", "E", "F", "G"]}]'

Notice that in the case "b" : "select" there is another variable c with the value being an array.

When I try running fromJSON(y) (in the jsonlite package) I get a data.frame where the 3rd column is a list:

> z <- fromJSON(y) 
> class(z)
[1] "data.frame"
> z %>% glimpse()
Observations: 3
Variables: 3
$ a (int) 1, 2, 3
$ b (chr) "select", "text", "select"
$ c (list) A, B, C, NULL, D, E, F, G

But when I convert it back to JSON (which is the format I want to send to the API), the c variable appears where it shouldn't be.

[{"a":1,"b":"select","c":["A","B","C"]},
 {"a":2,"b":"text","c":{}},
 {"a":3,"b":"select","c":["D","E","F","G"]}]

Is there another R data structure that could reliably get me from a data frame to the original JSON message using jsonlite (or something else)? How would it work?

解决方案

with jsonlite, you can do as follows:

z <- fromJSON(y, simplifyDataFrame = F)
toJSON(z, auto_unbox=T)

In addition, consider following case where last c element is ["D"]:

y2 <- '[{"a":1, "b":"select", "c":["A", "B", "C"]}, 
        {"a":2, "b":"text"}, 
        {"a":3, "b":"select", "c":["D"]}]'

z2 <- fromJSON(y2, simplifyDataFrame = F)
toJSON(z2, auto_unbox=T)

Result is this:

[{"a":1,"b":"select","c":["A","B","C"]},
 {"a":2,"b":"text"},
 {"a":3,"b":"select","c":"D"}]

This can be a trouble because the last c element is "D" not ["D"].

To prevent this, don's use auto_unbox. Use unbox carefully as follows:

z2 <- fromJSON(y2, simplifyDataFrame = F)
for(i in 1:length(z2)){
  z2[[i]][[1]] <- unbox(z2[[i]][[1]])
  z2[[i]][[2]] <- unbox(z2[[i]][[2]])
}
toJSON(z2)

这篇关于使用jsonlite或其他内容从R编码JSON表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆