Created
June 9, 2014 12:37
-
-
Save dacer/9bdcbea42262f79b509f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def json2simpleHash(json_str, prefix = '') | |
result = {} | |
json = JSON.parse(json_str) | |
json.each do |key, value| | |
if(value.is_a?(Hash)) | |
result.merge!(json2simpleHash(value.to_json, prefix+key+'_')) | |
else | |
result.merge!({"#{prefix}#{key}" => value}) | |
end | |
end | |
return result | |
end | |
def json2xml(json_str) | |
result = '<?xml version="1.0" encoding="utf-8"?>' | |
result += "\n" | |
result += '<resources>' | |
json2simpleHash(json_str).each do |key, value| | |
result += "\n" | |
result << "<string name=\"#{key}\">#{value}</string>" | |
end | |
result += "\n" | |
result += '</resources>' | |
end | |
#usage | |
json2xml(json_str) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment