我这样做
newzips=fromJSON("http://media.mongodb.org/zips.json")
Run Code Online (Sandbox Code Playgroud)
您可以在http://media.mongodb.org/zips.json上查看数据
并得到这样的
str(newzips)
List of 5
$ city : chr "ACMAR"
$ loc : num [1:2] -86.5 33.6
$ pop : num 6055
$ state: chr "AL"
$ _id : chr "35004\"}{\"city\":\"ADAMSVILLE\",\"loc\":[-86.959727,33.588437],\"pop\":10616,\"state\":\"AL\",\"_
Run Code Online (Sandbox Code Playgroud) 我的数据框包含以下数据:
Tester W1 W2 W3 A P WD(%) TS(Hrs.) AT(Hrs.) SU(%)
a 60 40 102 202 150 100 120 120 100
b 30 38 46 114 150 76 135 120 100
c 25 30 52 107 150 71 120 120 100
Run Code Online (Sandbox Code Playgroud)
通过使用该包,jsonlite我已转换为json格式:
{
"Tester": [ "a", "b", "c" ],
"W1": [ 60, 30, 25],
"W2": [ 40, 38, 30 ],
"W3": [ 102, 46, 52 ],
"A": [ 202, 114, 107 ],
"P": [ 150, 150, 150 ], …Run Code Online (Sandbox Code Playgroud) 我正在向想要使用http://json2csharp.com/将数据字符串转换为对象数组的C# 开发人员发送数据。我认为 jsonlite 包对此有好处,但他没有得到他想要的结果,当我检查生成的输出时,它并不是我所期望的。我对这种情况的解释是生成的输出
x <- list(
a=list( a=1,b='x',c=list("Foo","Bar"), d=as.Date("2015-01-01") ),
b=list( a=2,b='y',c=list("Hello","World"), d=as.Date("2014-12-31") ),
c=list( a=3,b='z',c=NULL, d=as.Date("2016-02-29") )
)
toJSON(x, pretty = FALSE )
Run Code Online (Sandbox Code Playgroud)
曾是
{"a":{"a":[1],"b":["x"],"c":[["Foo"],["Bar"]],"d":["2015-01-01"]},"b":{"a":[2],"b":["y"],"c":[["Hello"],["World"]],"d":["2014-12-31"]},"c":{"a":[3],"b":["z"],"c":{},"d":["2016-02-29"]}}
Run Code Online (Sandbox Code Playgroud)
它将所有子元素数据放入列表中,同时将父命名列表视为命名元素的对象。R 没有与 JSON 设计的相同类型的类型,所以这可能是 toJSON 的预期行为,但我想要更像
[{"a":1,"b":"x","c":["Foo","Bar"],"d":"2015-01-01"},{"a":2,"b":"y","c":["Hello","World"],"d":"2014-12-31"},{"a":3,"b":"z","c":{},"d":"2016-02-29"}]
Run Code Online (Sandbox Code Playgroud)
如何将 R 中的列表写入 jsonlite 中的 JSON 数组?
我正在使用的API需要我发送一条JSON消息,如:
y <- '[{"a":1, "b":"select", "c":["A", "B", "C"]},
{"a":2, "b":"text"},
{"a":3, "b":"select", "c":["D", "E", "F", "G"]}]'
Run Code Online (Sandbox Code Playgroud)
请注意,如果"b" : "select"有另一个变量c,其值为数组.
当我尝试运行fromJSON(y)(在jsonlite包中)时,我得到data.frame第3列是列表的位置:
> 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
Run Code Online (Sandbox Code Playgroud)
但是当我将其转换回JSON (这是我想发送给API的格式)时,c变量出现在它不应该的位置.
[{"a":1,"b":"select","c":["A","B","C"]},
{"a":2,"b":"text","c":{}},
{"a":3,"b":"select","c":["D","E","F","G"]}] …Run Code Online (Sandbox Code Playgroud) 我如何解析和处理已存在于数据框内的JSON?
样本数据:
df <- data.frame(
id = c("x1", "x2"),
y = c('[{"Property":"94","Value":"Error"},{"Property":"C1","Value":"Found Match"},{"Property":"C2","Value":"Address Mismatch"}]', '[{"Property":"81","Value":"XYZ"},{"Property":"D1","Value":"Blah Blah"},{"Property":"Z2","Value":"Email Mismatch"}]')
)
Run Code Online (Sandbox Code Playgroud)
我想提取,格式化并将列中的原始JSON y分成有序列,理想情况下是library(jsonlite).
提前致谢!
我正在尝试使用 htrr 和 jsonlite 包从美国海军天文台 Rest API中提取月出和设定时间。但是,我不断收到错误消息,请提供 MM/DD/YYYY 格式的日期。我相信我就是这样,所以很困惑。
library(httr)
library(jsonlite)
GET("https://aa.usno.navy.mil/api/rstt/oneday?date=09/20/2005&coords=47.60,-122.33")
Run Code Online (Sandbox Code Playgroud) 我正在使用 httr 库中的 POST 函数来获取一些数据,代码如下所示。
library(httr)
url = "https://xxxx:xxx@api.xxx/_search" #omitted for privacy
a = POST(url,body = query,encode = "json")
Run Code Online (Sandbox Code Playgroud)
该查询显示在下面的附录中。a$content给了我一大堆十六进制数字,我必须使用另一个函数才能获得一些有用的数据。
最终我希望通过使用获得一个数据框b = fromJSON(a$content)。到目前为止,为了获取任何数据,我必须使用:
chr<-function(n){rawToChar(as.raw(n))}
b = jsonlite::fromJSON(chr(a$content))
data = b$hits$hits$`_source`
Run Code Online (Sandbox Code Playgroud)
考虑到我是通过本地函数解析数据以获得最终数据,这似乎效率低下。所以我的问题如下:
附录:
query = '
{
"_source": [
"start","source.country_codes",
"dest.country_codes"
],
"size": 100,
"query": {
"bool": {
"must": [
{
"bool": {
"must_not": [
{
"range": {
"start": {
"lte": "2013-01-01T00:00:00"
}
}
},
{
"range": {
"start": {
"gt": "2016-05-19T00:00:00"
} …Run Code Online (Sandbox Code Playgroud) 假设我有这样的数据:
df <- data.frame(value = c("foo", "bar", "baz"))
Run Code Online (Sandbox Code Playgroud)
一个简单的方法toJSON让我这样:
[{"value":"foo"},{"value":"bar"},{"value":"baz"}]
Run Code Online (Sandbox Code Playgroud)
但是,如果我需要将值作为单个元素数组,我如何构造原始数据?我试过涉及到一些变化c(),list()和I().没有什么能让我得到这个输出:
[{"value":["foo"]},{"value":["bar"]},{"value":["baz"]}]
Run Code Online (Sandbox Code Playgroud) 我正在尝试显示此数据集 - > https://mtgjson.com/json/AllSets.json.zip
不过,我想展平数据,以便它不会作为一堆 JSON 数据嵌套在列表中、列表中、列表中。
更具体地说,我试图将数据显示为数据框,按$releaseDate(变量之一)的顺序排列。
到目前为止,这是我的尝试:
library(jsonlite)
library(tidyjson)
mtgdata <- fromJSON("~/path/to/file.json")
Run Code Online (Sandbox Code Playgroud)
mtgdata 的结果显示了这个列表列表:
summary(mtgdata)
Length Class Mode
UST 9 -none- list
UNH 10 -none- list
UGL 11 -none- list
pWOS 8 -none- list
pWOR 8 -none- list
pWCQ 8 -none- list
pSUS 8 -none- list
pSUM 10 -none- list
pREL 8 -none- list
pPRO 8 -none- list
pPRE 8 -none- list
pPOD 7 -none- list
pMPR 8 -none- list
pMGD 8 -none- list
pMEI 8 …Run Code Online (Sandbox Code Playgroud) 我想创建一个 JSON 对象,但根元素不应该是数组。我正在使用 jsonlite::toJSON,但我无法找到删除初始数组符号的方法。
FirstLevel=("FirstLevel")
df <- data.frame(FirstLevel)
jsonObj = toJSON(df,pretty = TRUE )
print(jsonObj)
Run Code Online (Sandbox Code Playgroud)
我明白了
[
{
"FirstLevel": "FirstLevel"
}
]
Run Code Online (Sandbox Code Playgroud)
但我需要看看
{
"FirstLevel": "FirstLevel"
}
Run Code Online (Sandbox Code Playgroud) 我有一个清单 my_list <- list(name="Fred", age="5")
我期待my_list %>% toJSON回来{"name": "Fred", "age": "5"}
相反,它返回 {"name":["Fred"],"age":["5"]}
如何在避免使用方括号的同时将此列表转换为 JSON?
我有采用以下格式的数据:
have <- structure(list(V1 = c(4L, 28L, 2L),
V2 = c("[{\"group\":1,\"topic\":\"A\"},{\"group\":1,\"topic\":\"B\"},{\"group\":2,\"topic\":\"C\"},{\"group\":2,\"topic\":\"T\"},{\"group\":2,\"topic\":\"U\"},{\"group\":3,\"topic\":\"V\"},{\"group\":3,\"topic\":\"D\"},{\"group\":3,\"topic\":\"R\"},{\"group\":4,\"topic\":\"A\"},{\"group\":4,\"topic\":\"Q\"},{\"group\":4,\"topic\":\"S\"},{\"group\":4,\"topic\":\"W\"},{\"group\":6,\"topic\":\"O\"},{\"group\":6,\"topic\":\"P\"},{\"group\":6,\"topic\":\"E\"},{\"group\":6,\"topic\":\"F\"},{\"group\":6,\"topic\":\"G\"},{\"group\":6,\"topic\":\"H\"},{\"group\":6,\"topic\":\"I\"},{\"group\":6,\"topic\":\"J\"},{\"group\":6,\"topic\":\"K\"},{\"group\":6,\"topic\":\"L\"},{\"group\":6,\"topic\":\"M\"},{\"group\":6,\"topic\":\"N\"}]",
"[]",
"[{\"group\":2,\"topic\":\"C\"},{\"group\":3,\"topic\":\"D\"},{\"group\":6,\"topic\":\"O\"},{\"group\":6,\"topic\":\"P\"},{\"group\":6,\"topic\":\"E\"},{\"group\":6,\"topic\":\"G\"},{\"group\":6,\"topic\":\"M\"}]")
),
row.names = c(NA, 3L),
class = "data.frame")
Run Code Online (Sandbox Code Playgroud)
的内容V2是每一行的嵌套分组,如[{"group":1,"topic":"A"},{"group":1,"topic":"B"}...]
我想获得一个宽数据框,其中also_have每行的 group+topic 的每个组合都有一个指示符 (1/0)(请参阅 参考资料)。像这样的东西:
# A tibble: 3 x 4
id topic_id_1 topic_id_2 topic_id_3 topic_id_4 ...
<dbl> <dbl> <dbl> <dbl>
1 4 1 1 0
2 28 0 0 0
3 2 0 0 0
Run Code Online (Sandbox Code Playgroud)
第一步是解析json。
我可以使用purrr::map(have$V2, jsonlite::fromJSON)取消嵌套到列表中,但我不确定如何将V1列(我们可能会重命名为id)绑定到结果列表的每个元素(请注意,列表元素二是空的,因为它V1==28是空的)。下面是添加了id( V1) …
我想使用 R 的 jsonlite 包创建 JSON 数据以使用Python加载到DynamoDB。我希望数据位于如下所示的结构中。如何在 R 中创建它?我尝试创建一个数据框,其中一列是列表,并将数据框更改为 json,但结果不是所需的格式。我还尝试转换包含列表的列表,但输出 json 的结构不是我想要的。
[
{
"ID": 100,
"title": "aa",
"more": {
"interesting":"yes",
"new":"no",
"original":"yes"
}
},
{
"ID": 110,
"title": "bb",
"more": {
"interesting":"no",
"new":"yes",
"original":"yes"
}
},
{
"ID": 200,
"title": "cc",
"more": {
"interesting":"yes",
"new":"yes",
"original":"no"
}
}
]
Run Code Online (Sandbox Code Playgroud)
这是我的示例数据和我尝试过的:
library(jsonlite)
ID=c(100,110,200)
Title=c("aa","bb","cc")
more=I(list(Interesting=c("yes","no","yes"),new=c("no","yes","yes"),original=c("yes","yes","no")))
a=list(ID=ID,Title=Title,more=more)
a=toJSON(a)
write(a,"temp.json") # this does not give the structure I want
Run Code Online (Sandbox Code Playgroud)