Skip to content

Instantly share code, notes, and snippets.

@dacer
Created June 9, 2014 12:37
Show Gist options
  • Save dacer/9bdcbea42262f79b509f to your computer and use it in GitHub Desktop.
Save dacer/9bdcbea42262f79b509f to your computer and use it in GitHub Desktop.
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