gpt4 book ai didi

android - 从 Kotlin android 中的 json 字符串中删除换行符\n

转载 作者:行者123 更新时间:2023-12-02 13:34:53 37 4
gpt4 key购买 nike

我正在尝试从 json 字符串中删除\n 字符,因为它不允许我在我的 TextView 上设置文本。我尝试了不同的示例,例如 how to remove attributes from json using Kotlin and jackson ObjectMapper但它没有帮助我。我想使用 Gson 将 json 中的值分配给 textviews,但由于\n 字符,它会变为 null。

我也尝试用 .toRegex 方法替换,但它并没有帮助我。

这是我的 MainActivity 代码

class MainActivity : AppCompatActivity() {

private var mGitHubApiCall: GithubApiCall? = null
var result: String?= null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mGitHubApiCall = GitHubCVService.gitHubService
mGitHubApiCall!!.getData().enqueue(object : Callback<GithubResp> {
override fun onResponse(call: Call<GithubResp>, response: Response<GithubResp>) {
if (response.isSuccessful) {
val msg = response.body()?.files?.cvDummyJson?.content
result= msg+""
Log.e("data", result)
val data= Gson().fromJson(result, Data::class.java)
user_name.text= data.name
summary_desc.text= data.summary
} else {
Log.e("data", "error")
}
}

override fun onFailure(call: Call<GithubResp>, t: Throwable) {
Log.e("data", "Fail to call")
}

})


}

我的 json 来了
{\n
"data":{\n
"name":"Maria",\n
"summary":"Likable and dedicated IT consultant with over 5 years of experience in a fast-paced fin-tech company. Eager to offer superb analytical and computer skills to help ABC Inc grow its client base. In previous roles recognized for top company-wide quality satisfaction rating.",\n
"skills":[\n
{\n
"type":"Design Languages",\n
"languages":[\n
"HTML",\n
"XML",\n
"CSS"\n
]\n
},\n
{\n
"type":"Language",\n
"languages":[\n
"Java",\n
"C#",\n
"C++",\n
"Kotlin"\n
]\n
},\n
{\n
"type":"Scripting Language",\n
"languages":[\n
"Javascript"\n
]\n
},\n
{
"type":"Server Language",\n
"languages":[\n
"Php"\n
]\n
},\n
{\n
"type":"Operating System",\n
"languages":[\n
"Windows",\n
"Mac",\n
"Android"\n
]\n
},\n
{\n
"type":"Database",\n
"languages":[\n
"Sqlite",\n
"SQL",\n
"MySQL"\n
]\n
},\n
{\n
"type":"Frameworks", \n
"languages":[\n
"Netbeans",\n
"Eclipse",\n
"ADT-Bundle"\n
]\n
}\n
],\n
"education_bg":[\n
{\n
"position":"Post-Graduate in IBM",\n
"Major":"International Business",\n
"from":"2018",\n
"to":"2019",\n
"institute_name":"Douglas College"\n
},\n
{\n
"position":"Post-Graduate in ICT",\n
"Major":"Information Technology",\n
"from":"2017",\n
"to":"2018",\n
"institute_name":"Douglas College"\n
},\n
{\n
"position":"Bachelors in CSE",\n
"Major":"Computer Science",\n
"from":"2010",\n
"to":"2014",\n
"institute_name":"Douglas College"\n
}\n
]\n
}\n
}

我想将此 json 字符串中的值设置到我的 textview 中,该值将变为 null 并且不显示任何内容。

提前致谢

最佳答案

我认为您的 JSON 第一次是 String 并且您想使用 Jackson 将其转换为 JSON。所以首先你可以用“”替换你的字符串“\n”。然后将您的 JSON 字符串解析为 JSON 对象。检查这个简单的例子并在你的情况下试试这个。希望它会帮助你。

fun main(){
val myobj="{\nname: \"John\",\n age: 31,\n city: \"New York\"}"
val result=myobj.replace("\n","")
print(result)
}

关于android - 从 Kotlin android 中的 json 字符串中删除换行符\n,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58460409/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com