开发者

convert xml attribute (which is a hash) to a Ruby hash

开发者 https://www.devze.com 2023-04-01 22:27 出处:网络
I have a node that looks like this, in the xml file: <property name=\"nameA\" value=\"{ \"keyA\": \"valueA\", \"keyB\": \"valueB\" }\"/>

I have a node that looks like this, in the xml file:

<property name="nameA" value="{ "keyA": "valueA", "keyB": "valueB" }"/>

I create the nokogiri object, search it for the value of that node, and get th开发者_Go百科is:

{ "keyA": "valueA", "keyB": "valueB" }

I need to modify valueA. Do I convert this to a Ruby hash, modify it, and then convert it back? How would I do that? Thanks.


This looks like JSON.

gem install json

Then:

require 'json'
hash = JSON.parse('{ "keyA": "valueA", "keyB": "valueB" }')
hash['keyC'] = 'valueC';
attr = hash.to_json
0

精彩评论

暂无评论...
验证码 换一张
取 消