如何使用Ruby on Rails转换字符串“to_json”和“to_xml”?

时间:2022-04-24 21:49:50

I am using Ruby on Rails 3 and I would like, in some way, to convert a string to_json and to_xml.

我正在使用Ruby on Rails 3,我想以某种方式将字符串转换为_json和to_xml。

Just to know, I need to return that string in a Rack method in this way:

只是想知道,我需要用机架方法返回那个字符串:

[404, {"Content-type" => "application/json"}, ["Bad request"]]
# or
[404, {"Content-type" => "application/xml"}, ["Bad request"]]

However what I need is only to convert that string to_json and to_xml? How is it possible do that?

但是,我需要的只是转换字符串to_json和to_xml?怎么可能呢?

3 个解决方案

#1


5  

Sometimes you must add a require 'json' in your file (after installing the gem, JSON implementation for Ruby ) and do:

有时,您必须在文件中添加一个需要的“json”(在安装gem、Ruby的json实现之后),并执行以下操作:

JSON.parse("Bad request").to_json   

or you could try:

或者你可以试试:

ActiveSupport::JSON.encode("Bad request").to_json

But in your case maybe the best approach is respond correctly:

但在你的例子中,也许最好的方法是正确地回答:

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @somearray }
  format.json  { render :json => @somearray }
end

Alternatively you could do:

或者你可以做的:

  mystring_json = '{"bar":"foo"}'
  [404, {'Content-Type' => 'application/json'}, [mysrting_json]] #json stuff
  mystring_xml = '<?xml><bar>foo</bar>'
  [404, {'Content-Type' => 'application/xml'}, [mysrting_xml]]  #xml stuff

#2


2  

In your controller, just pass in the XML string:

在控制器中,只需传入XML字符串:

render xml: "<myxml><cool>fff</cool></myxml>"

呈现xml:“< myxml > <酷> fff < /酷> < / myxml > "

#3


1  

JSON follows JavaScript syntax, so a string in JSON is easy:

JSON遵循JavaScript语法,因此JSON中的字符串很容易:

[404, {"Content-type" => "application/json"}, ["'Bad request'"]]

As for XML, the answer is not so simple. You'd have to decide what tag structure you want to use and go from there. Remember that an XML doc has, at a minimum, a root tag. So, you could return an XML doc like this:

至于XML,答案并不简单。您必须决定要使用什么样的标记结构,然后从那里开始。请记住,XML文档至少有一个根标记。因此,您可以返回这样的XML文档:

<?xml version="1.0" encoding="utf-8"?>
<response>Bad request</response>

One way to do this would be to use the Builder gem:

一种方法是使用Builder gem:

http://builder.rubyforge.org/

http://builder.rubyforge.org/

However, I'm not entirely certain why you would need to return a string all by itself as JSON or XML. JSON and XML are typically used for transmitting structured data, e.g. arrays, nested data, key-value pairs, etc.. Whatever your client is, it could presumably just interpret the string as-is, without any JSON or XML ecoding, no?

然而,我并不完全确定为什么需要返回一个单独作为JSON或XML的字符串。JSON和XML通常用于传输结构化数据,例如数组、嵌套数据、键-值对等。无论您的客户端是什么,它都可能只是按原样解释字符串,没有任何JSON或XML编码,不是吗?

#1


5  

Sometimes you must add a require 'json' in your file (after installing the gem, JSON implementation for Ruby ) and do:

有时,您必须在文件中添加一个需要的“json”(在安装gem、Ruby的json实现之后),并执行以下操作:

JSON.parse("Bad request").to_json   

or you could try:

或者你可以试试:

ActiveSupport::JSON.encode("Bad request").to_json

But in your case maybe the best approach is respond correctly:

但在你的例子中,也许最好的方法是正确地回答:

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @somearray }
  format.json  { render :json => @somearray }
end

Alternatively you could do:

或者你可以做的:

  mystring_json = '{"bar":"foo"}'
  [404, {'Content-Type' => 'application/json'}, [mysrting_json]] #json stuff
  mystring_xml = '<?xml><bar>foo</bar>'
  [404, {'Content-Type' => 'application/xml'}, [mysrting_xml]]  #xml stuff

#2


2  

In your controller, just pass in the XML string:

在控制器中,只需传入XML字符串:

render xml: "<myxml><cool>fff</cool></myxml>"

呈现xml:“< myxml > <酷> fff < /酷> < / myxml > "

#3


1  

JSON follows JavaScript syntax, so a string in JSON is easy:

JSON遵循JavaScript语法,因此JSON中的字符串很容易:

[404, {"Content-type" => "application/json"}, ["'Bad request'"]]

As for XML, the answer is not so simple. You'd have to decide what tag structure you want to use and go from there. Remember that an XML doc has, at a minimum, a root tag. So, you could return an XML doc like this:

至于XML,答案并不简单。您必须决定要使用什么样的标记结构,然后从那里开始。请记住,XML文档至少有一个根标记。因此,您可以返回这样的XML文档:

<?xml version="1.0" encoding="utf-8"?>
<response>Bad request</response>

One way to do this would be to use the Builder gem:

一种方法是使用Builder gem:

http://builder.rubyforge.org/

http://builder.rubyforge.org/

However, I'm not entirely certain why you would need to return a string all by itself as JSON or XML. JSON and XML are typically used for transmitting structured data, e.g. arrays, nested data, key-value pairs, etc.. Whatever your client is, it could presumably just interpret the string as-is, without any JSON or XML ecoding, no?

然而,我并不完全确定为什么需要返回一个单独作为JSON或XML的字符串。JSON和XML通常用于传输结构化数据,例如数组、嵌套数据、键-值对等。无论您的客户端是什么,它都可能只是按原样解释字符串,没有任何JSON或XML编码,不是吗?