XML转换为JSON R

XML转换为JSON R,json,r,xml,Json,R,Xml,在将XML转换为JSON时,R中的包似乎不能正常工作。我用“XML”包尝试了RJSONIO、rjson和jsonlite。我首先解析XML并使用XML::xmlToList()将其转换为列表,然后使用这3个包中的toJSON()将其转换为JSON 我的XML文件: <?xml version="1.0" encoding="utf-8"?> <votes> <row Id="1" PostId="1" VoteTypeId="2" CreationDate="2

在将XML转换为JSON时,R中的包似乎不能正常工作。我用“XML”包尝试了RJSONIO、rjson和jsonlite。我首先解析XML并使用XML::xmlToList()将其转换为列表,然后使用这3个包中的toJSON()将其转换为JSON

我的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<votes>
  <row Id="1" PostId="1" VoteTypeId="2" CreationDate="2014-05-13T00:00:00.000" />
  <row Id="2" PostId="1" VoteTypeId="2" CreationDate="2014-05-13T00:00:00.000" />
  <row Id="3" PostId="3" VoteTypeId="2" CreationDate="2014-05-13T00:00:00.000" />
</votes>
这显然是错误的,因为重复的字段名

从jsonlite转换的JSON文件:

{"row":["1","1","2","2014-05-13T00:00:00.000"],
 "row.1":["2","1","2","2014-05-13T00:00:00.000"],
 "row.2":["3","3","2","2014-05-13T00:00:00.000"]}
这很奇怪,因为子文档数组中应该只有一个字段名“row”,而不是递增的“rows”数组。它甚至没有字段名

从rjson转换的JSON文件:

{
 "row": {
 "Id": "1",
"PostId": "1",
"VoteTypeId": "2",
"CreationDate": "2014-05-13T00:00:00.000" 
},
"row": {
 "Id": "2",
"PostId": "1",
"VoteTypeId": "2",
"CreationDate": "2014-05-13T00:00:00.000" 
}
}
理想的JSON文件如下所示:

{"votes" : {
    "row" : [
        {
            "Id" : "1",
            "PostId" : "1",
            "VoteTypeId" : "2",
            "CreationDate" : "2014-05-13T00:00:00.000"
        },
        {
            "Id" : "2",
            "PostId" : "1",
            "VoteTypeId" : "2",
            "CreationDate" : "2014-05-13T00:00:00.000"
        }
       ]
      }
}

寻找解决方案。非常感谢您的帮助。

xml2
jsonlite
为您提供了大部分帮助,但您甚至没有向我们展示您知道的R code,更不用说真正尝试了解决方案,因此这里发布了一个部分解决方案,它可以帮助其他人:

library(xml2)
library(jsonlite)

read_xml('<?xml version="1.0" encoding="utf-8"?>
<votes>
  <row Id="1" PostId="1" VoteTypeId="2" CreationDate="2014-05-13T00:00:00.000" />
  <row Id="2" PostId="1" VoteTypeId="2" CreationDate="2014-05-13T00:00:00.000" />
  <row Id="3" PostId="3" VoteTypeId="2" CreationDate="2014-05-13T00:00:00.000" />
</votes>') -> doc

x <- xml2::as_list(doc) 

xl <- lapply(x, attributes)

toJSON(xl, pretty = TRUE, auto_unbox = TRUE)
## {
##   "row": {
##     "Id": "1",
##     "PostId": "1",
##     "VoteTypeId": "2",
##     "CreationDate": "2014-05-13T00:00:00.000"
##   },
##   "row.1": {
##     "Id": "2",
##     "PostId": "1",
##     "VoteTypeId": "2",
##     "CreationDate": "2014-05-13T00:00:00.000"
##   },
##   "row.2": {
##     "Id": "3",
##     "PostId": "3",
##     "VoteTypeId": "2",
##     "CreationDate": "2014-05-13T00:00:00.000"
##   }
## } 

显示用于获取错误JSON字符串的代码以及XML转换产生的数据,以便我们可以帮助您正确地解决问题。感谢@hrbrmstr的回答。正如我在文章中所说,我已经尝试过jsonlite,但输出并不理想,正如您在这里看到的,理想的输出是一个字段名“row”,其中包含子文档数组,而不是单个文档中的多个递增“row”。
{"votes" : {
    "row" : [
        {
            "Id" : "1",
            "PostId" : "1",
            "VoteTypeId" : "2",
            "CreationDate" : "2014-05-13T00:00:00.000"
        },
        {
            "Id" : "2",
            "PostId" : "1",
            "VoteTypeId" : "2",
            "CreationDate" : "2014-05-13T00:00:00.000"
        }
       ]
      }
}
library(xml2)
library(jsonlite)

read_xml('<?xml version="1.0" encoding="utf-8"?>
<votes>
  <row Id="1" PostId="1" VoteTypeId="2" CreationDate="2014-05-13T00:00:00.000" />
  <row Id="2" PostId="1" VoteTypeId="2" CreationDate="2014-05-13T00:00:00.000" />
  <row Id="3" PostId="3" VoteTypeId="2" CreationDate="2014-05-13T00:00:00.000" />
</votes>') -> doc

x <- xml2::as_list(doc) 

xl <- lapply(x, attributes)

toJSON(xl, pretty = TRUE, auto_unbox = TRUE)
## {
##   "row": {
##     "Id": "1",
##     "PostId": "1",
##     "VoteTypeId": "2",
##     "CreationDate": "2014-05-13T00:00:00.000"
##   },
##   "row.1": {
##     "Id": "2",
##     "PostId": "1",
##     "VoteTypeId": "2",
##     "CreationDate": "2014-05-13T00:00:00.000"
##   },
##   "row.2": {
##     "Id": "3",
##     "PostId": "3",
##     "VoteTypeId": "2",
##     "CreationDate": "2014-05-13T00:00:00.000"
##   }
## } 
xml_find_all(doc, "//votes/row") %>% 
  map_chr(~{
    toJSON(as.list(xml_attrs(.x)), auto_unbox = TRUE, pretty = TRUE)
  }) %>% 
  paste0(collapse=",\n") %>% 
  gsub("[\n]", "\n    ", .) %>% 
  sprintf('{ "votes" : {\n  row" : [\n    %s]\n  }\n}', .) %>% 
  cat()

## { "votes" : {
##   row" : [
##     {
##       "Id": "1",
##       "PostId": "1",
##       "VoteTypeId": "2",
##       "CreationDate": "2014-05-13T00:00:00.000"
##     },
##     {
##       "Id": "2",
##       "PostId": "1",
##       "VoteTypeId": "2",
##       "CreationDate": "2014-05-13T00:00:00.000"
##     },
##     {
##       "Id": "3",
##       "PostId": "3",
##       "VoteTypeId": "2",
##       "CreationDate": "2014-05-13T00:00:00.000"
##     }]
##   }
## }