0
我试图将XML转换为JSON,但我在这样做时丢失了数据。Rails将XML转换为json
在XML我最两件事护理:
- 的
cint:variable-id="11734028"
- 真实文本值 -
American Indian or Alaska Native
当我转换使用Hash.from_xml(xml).to_json
XML以JSON它似乎失去了价值标签内的信息。
的XML是这样的:
xml = '<?xml version="1.0" encoding="utf-8" standalone="no"?>
<surveys xmlns:cint="http://www.cint.com/">
<sss version="2.0" languages="x-native" xmlns:cint="http://www.cint.com/">
<date>2017-06-28</date>
<time>17:00:35</time>
<survey>
<title>Ethnicity<text xml:lang="x-native">Ethnicity</text></title>
<record ident="c" cint:category-id="63608">
<variable ident="451539" type="single" cint:sort-order="1" cint:hide-on="Registration" cint:country-language-question-id="2222">
<name>Ethnicity - US & Canada only</name>
<label>How would you define yourself as it relates to your ethnicity? (US and Canada only)<text xml:lang="x-native">How would you define yourself as it relates to your ethnicity?</text></label>
<position start="1" />
<values>
<value code="1" cint:variable-id="11734028" cint:global-variable-id="2740" cint:country-language-variable-id="36688">American Indian or Alaska Native<text xml:lang="x-native">American Indian or Alaska Native </text></value>
<value code="2" cint:variable-id="11734029" cint:global-variable-id="2741" cint:country-language-variable-id="36689">Asian<text xml:lang="x-native">Asian </text></value>
<value code="3" cint:variable-id="11734030" cint:global-variable-id="2742" cint:country-language-variable-id="36690">Black or African American<text xml:lang="x-native">Black or African American </text></value>
<value code="4" cint:variable-id="11734031" cint:global-variable-id="2743" cint:country-language-variable-id="36691">Hispanic or Latino<text xml:lang="x-native">Hispanic or Latino </text></value>
<value code="5" cint:variable-id="11734032" cint:global-variable-id="2744" cint:country-language-variable-id="36692">Native Hawaiian or Pacific Islander<text xml:lang="x-native">Native Hawaiian or Pacific Islander </text></value>
<value code="6" cint:variable-id="11734033" cint:global-variable-id="2745" cint:country-language-variable-id="36693">White<text xml:lang="x-native">White </text></value>
<value code="98" cint:variable-id="11734034" cint:global-variable-id="2746" cint:country-language-variable-id="36694">Other<text xml:lang="x-native">Other </text></value>
<value code="99" cint:variable-id="11734035" cint:global-variable-id="2747" cint:country-language-variable-id="36695">I prefer not to answer<text xml:lang="x-native">Prefer not to say </text></value>
</values>
</variable>
</record>
</survey>
</sss>
</surveys>'
输出JSON是这样的:
"{\"surveys\":{\"xmlns:cint\":\"http://www.cint.com/\",\"sss\":{\"version\":\"2.0\",\"languages\":\"x-native\",\"xmlns:cint\":\"http://www.cint.com/\",\"date\":\"2017-06-28\",\"time\":\"17:00:35\",\"survey\":{\"title\":\"Ethnicity\",\"record\":{\"ident\":\"c\",\"cint:category_id\":\"63608\",\"variable\":{\"ident\":\"451539\",\"type\":\"single\",\"cint:sort_order\":\"1\",\"cint:hide_on\":\"Registration\",\"cint:country_language_question_id\":\"2222\",\"name\":\"Ethnicity - US \\u0026 Canada only\",\"label\":\"How would you define yourself as it relates to your ethnicity? (US and Canada only)\",\"position\":{\"start\":\"1\"},\"values\":{\"value\":[\"American Indian or Alaska Native\",\"Asian\",\"Black or African American\",\"Hispanic or Latino\",\"Native Hawaiian or Pacific Islander\",\"White\",\"Other\",\"I prefer not to answer\"]}}}}}}}"
任何想法如何,我可以将此转换为JSON没有价值标签内丢失的信息?
我没有试过Nokogiri之前。任何快速示例让我走上正轨? –
Nokogiri网站上的文档非常好,但为了检索您的第一个值,您可以执行以下操作:'xml_doc = Nokogiri :: XML(xml); val = xml_doc.xpath('// value')。first;放入val.text' – Brian