XML转换为JSON R [英] XML convert to JSON R

查看:78
本文介绍了XML转换为JSON R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R中的软件包在将XML转换为JSON时似乎无法正常工作.我已经尝试过"XML"包的RJSONIO,rjson和jsonlite.我首先解析XML,然后使用XML :: xmlToList()将其转换为列表,然后使用这3个包中的toJSON()将其转换为JSON.

The packages in R doesn't seem to work properly in conversion of XML to JSON. I've tried RJSONIO, rjson and jsonlite with 'XML' package. I first parsed the XML and convert it into list using XML::xmlToList() and convert them to JSON using toJSON() from those 3 packages.

我的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>

我的源代码:

library(XML)
library(RJSONIO)
library(rjson)
library(jsonlite)

xml_parse <- xmlTreeParse("~/Downloads/test.xml", useInternalNodes=TRUE)
xml_root <- xmlRoot(xml_parse)
xml_list <- xmlToList(xml_root, simplify = TRUE)

#jsonlite package
xml_jsonlite <- jsonlite::toJSON(xml_list)
write(xml_jsonlite, "test_jsonlite.json")

#RJSONIO package
xml_rjsonio <- RJSONIO::toJSON(xml_list)
write(xml_rjsonio, "test_rjsonio.json")

#rjson package
xml_rjson <- RJSONIO::toJSON(xml_list)
write(xml_rjson, "test_rjson.json")

从RJSONIO转换的JSON文件:

Converted JSON file from RJSONIO:

{
"row": {
    "Id": "98",
    "PostId": "10",
    "VoteTypeId": "2",
    "CreationDate": "2014-05-14T00:00:00.000" 
 },
"row": {
    "Id": "99",
    "PostId": "7",
    "VoteTypeId": "5",
    "UserId": "111",
    "CreationDate": "2014-05-14T00:00:00.000" 
 }
}

显然是错误的,因为字段名重复.

Which is clearly wrong because of duplicate field name.

从jsonlite转换的JSON文件:

Converted JSON file from jsonlite:

{"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"数组.它甚至没有字段名.

Which is weird because there should be only one field name "row" with array of subdocuments instead of incrementing array of "rows". It doesn't even have field names in it.

从rjson转换的JSON文件:

Converted JSON file from rjson:

{
 "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文件将是这样的:

The ideal JSON file would be as such:

{"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"
        }
       ]
      }
}

寻找解决方案.感谢您的帮助.

Looking for solution. Any help is appreciated.

推荐答案

xml2jsonlite可以帮助您解决大部分问题,但您甚至没有向我们展示,您知道R代码更不用说真正尝试了一种解决方案为此,这里发布了部分解决方案,可以为其他人提供帮助:

xml2 and jsonlite get you most of the way there but you haven't even shown us you know R code let alone have really attempted a solution for this, so here's a partial solution posted so it can help others:

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"
##   }
## } 

根据您的评论

您想要的不是数据的结构方式.这意味着,如果您需要某些东西,就不能使用罐头香草实用程序.

What you want isn't how the data is structured. Which means if you want something, you can't use canned, vanilla utilities.

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"
##     }]
##   }
## }

这篇关于XML转换为JSON R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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