1

我有一个 Rails 应用程序,其中有一个模型帖子。具有两个属性的帖子模型 - “标题”和“内容”。我正在为我的内容使用ckeditor。这是我的 ckditor 快照

在此处输入图像描述

当我用粗体、斜体、下划线等保存一些东西时,例如“hi how r u”。然后它会保存这样的帖子内容<b><i><u>hi how r u</u><i></b>。我知道rails中的解决方案。我正在使用 <%= raw(@posts.content) %> 来解决这个问题。但现在我的问题不同了。我想使用 json 从数据库中获取所有帖子内容。我正在这样做..

def index
  @posts = Post.all
  respond_with(@posts) do |format|
    format.json { render json: @post_names = {:post => @posts.as_json(:only=> :content)} }
  end
 end 

现在,当我通过时http://localhost:3000/posts.json,它显示为这样

{"post":[{"content":"hi"},{"content":"hi"},{"content":"<img src=\"https://www.ginfy.com/img/logo.png\">"},{"content":"hi"},{"content":"my health is not good. Please pray for me..<br>"},{"content":"my health is not good. Please pray for me<br>"},{"content":"hello"},{"content":"<img src=\"http://www.ibettertechnologies.com/img/logo.png\">"},{"content":"<u><i><b>hi how r u?</b></i></u><br>"},{"content":"sdfsd"},{"content":"i am going to home. Please pray for my safe jouney"},{"content":"job"},{"content":"dsfdfs"},{"content":"fddf"},{"content":"jd dsbdsj djhj dsjhdfjks"},{"content":"ddd gd fgdfdg"},{"content":"dfslkdfskldfskl dskljdfskldf dskljdfskldfjslk"},{"content":"please pray for me"},{"content":"dfdf"},{"content":"cvxvxdfs"},{"content":"hdbhjds dsjks"},{"content":"s"},{"content":"hi"},{"content":"hu"}]}

我想在android中获取这个json。如何在 json 中使用 <%= raw() %> 函数来获取正确的格式化值。

4

1 回答 1

1

您必须使用Html.fromHtml()函数来转义 HTML 标签。你可以这样做..

<TextView 
   android:id="@+id/text2" 
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentLeft="true"
   android:layout_margin="5dp"
   android:textColor="@android:color/black"
   android:textSize="20sp"
   android:textStyle="bold" />


   text2.setText(Html.fromHtml(app.getTitle()).toString());
于 2013-06-18T05:33:01.427 回答