如何在从 Json 获取数据到 android 应用程序时删除标签/特殊字母?

Posted

技术标签:

【中文标题】如何在从 Json 获取数据到 android 应用程序时删除标签/特殊字母?【英文标题】:How to remove tags/ special letters while fetching data from Json to an android app? 【发布时间】:2021-10-18 13:29:22 【问题描述】:

我收到这样的 JSON 响应 ->

您可以在这里查看完整的 JSON 响应 -> https://api.nasa.gov/techtransfer/patent/?engine&api_key=DEMO_KEY

这也是我的数据类的样子 ->

          @Parcelize
             data class Patent(
             val count: Int,
             val page: Int,
             val perpage: Int,
             val results: List<List<String>>,
             val total: Int
                 ) : Parcelable

我使用 Retrofit 解析数据。

这是界面->

    interface NasaInterface 

@GET("techtransfer/patent/?telescope&api_key=$API_KEY")
fun getNasaPatents(): Call<Patent>   
              
     
        object NasaService 
     
         val nasaInterface: NasaInterface

         init 
         val retrofit = Retrofit.Builder()
                  .baseUrl(BASE_URL)
                  .addConverterFactory(GsonConverterFactory.create())
                  .build()

    nasaInterface = retrofit.create(NasaInterface::class.java)
   

这是获取数据的功能 ->

                private fun callingAdapter() 
    val nasaPatents = NasaService.nasaInterface.getNasaPatents()

    nasaPatents.enqueue(object : Callback<Patent>, iPostAdapter 
        override fun onResponse(call: Call<Patent>, response: Response<Patent>) 
            val nasaPatentsi = response.body()

            if (nasaPatentsi != null) 

                adapter = PatentAdapter(this@MainFragment, nasaPatentsi, this)
                recyclerViewItems!!.adapter = adapter
            

        

        override fun onFailure(call: Call<Patent>, t: Throwable) 
            Log.d("pERROR", "$t , $call")
            progressLoaderAdp!!.visibility = View.VISIBLE

        

我想删除那些像 "span class..." 这样的标签。 当你查看那个 JSON 响应时,你会看到所有重要的东西都来自 Result Key。我想知道我们是否可以从 Result 键的值中删除这些标签?

【问题讨论】:

这可能会有所帮助:***.com/questions/6502759/… 谢谢它的工作。 【参考方案1】:

我不会删除 html 代码,而是将文本设置为 HTML。

Html.fromHtml("NASA TEXT WITH HTML", flag)

【讨论】:

感谢您的回答我没有 html 经验,请您详细说明或分享任何有用的资源来做同样的事情。我还有一个问题,如果 JSON 得到更新或从 nasa 发生变化怎么办?你的解决方案还能用吗? 有些HTML标签是android不支持的,在这里你可以找到完整的列表。 commonsware.com/blog/Android/2010/05/26/… 我的解决方案是因为您试图在 textview 上设置 HTML 文本。另一种选择是删除所有 HTML 标记,但为此,您需要使用正则表达式,这不是一个好主意。如果 NASA 使用更多 HTML 标记更新文本,并且这些标记不受支持(如列表),您将看到纯文本而不是列表。 伙计,我仍然不知道如何删除它。我也邮寄给了美国宇航局。看到这里我把这个上传到 playstore (play.google.com/store/apps/…) 我必须尽快解决这个错误。 你能把你设置文本的代码片段粘贴到texview吗? 现在检查我已经添加了。

以上是关于如何在从 Json 获取数据到 android 应用程序时删除标签/特殊字母?的主要内容,如果未能解决你的问题,请参考以下文章

如何在android中使用OkHttp从web api获取json数据到RecyclerView

如何从php服务器获取json数据到android mobile

如何使用json解析android在listview中获取图像

我被困在从 JSON API 获取数据中,它有点复杂和嵌套,我想从“abilities”数组中获取“resource_uri”

如何配置 azure 应用以在从应用中删除用户时发送通知

Ionic Angular Mobile App 在从项目中的文件中获取和显示 json-array 数据时显示错误---