Removing newline \n from json String in Kotlin android

Viewed 12764

I am trying to remove \n characters from the json String as it is not allowing me to set text on my text views. I tried different examples like how to remove attributes from json using Kotlin and jackson ObjectMapper but it not helping me out. I want to assign values from the json using Gson to the textviews but it is coming null because of \n characters.

I tried replace with .toRegex method as well but it did not help me out.

This is my MainActivity Code

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")
        }

    })


}

My json is coming as

{\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
}

I want to set values from this json string to my textview which is coming null and showing nothing.

Thanks in advance

1 Answers

I think your JSON is as String the first time and you want to convert as JSON using Jackson. So first you can replace your string "\n" with " ". Then parse your JSON string to a JSON object. Check this simple example and try this in your case. Hope it will help you.

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