我想知道是否有办法解码类似JSON的字符串.
我有字符串:
'{ hotel: { id: "123", name: "hotel_name"} }'
Run Code Online (Sandbox Code Playgroud)
它不是有效的JSON字符串,因此我无法使用python API直接对其进行解码.Python只接受字符串化的JSON字符串,如:
'{ "hotel": { "id": "123", "name": "hotel_name"} }'
Run Code Online (Sandbox Code Playgroud)
其中属性被引用为字符串.
我正在尝试在Rails 4中应用此嵌入式视图技巧https://gist.github.com/jturkel/7917985#file-inline_view_model-rb,但是在我的视图中有一个列将返回从postgresql生成的数组建立功能array_agg()。
class Product < ActiveRecord::Base
defult_scope { set_from_clause }
def self.set_from_clause
query = Product.joins(:tags)
.group("products.id")
.select("products.id", "Array_agg('tags.id') AS tag_ids")
from(query, table_name)
end
def self.columns
[
ActiveRecord::ConnectionAdapters::Column.new('product_id', nil, ActiveRecord::Type::String.new),
ActiveRecord::ConnectionAdapters::Column.new('tag_ids', nil, ActiveRecord::Type::Integer.new),
]
end
end
Run Code Online (Sandbox Code Playgroud)
此示例代码无法正常工作,因为Array_agg无法正确键入由返回的值。我想知道是否有一种方法可以使tag_ids示例中的技巧起作用。