I'm trying to parse JSON in Kotlin manually without the use of a 3rd party JSON parser (unlike the other questions that were already asked). I'm building Udacity's "popular movies" app with the themovieDb api to be comfortable building Android apps with Kotlin because I've never used Kotlin before. For the first section, I want to display the movie posters in a 3 column grid. This is the position of the poster_path object in the API:
{
...
"poster_path": "...jpg",
}
In Java, I would normally create a JSONObject and then use methods like getString or getInt on that object to retrieve the specific information I need. How does one go about this in Kotlin?
What I have so far is an app that repeats the same image on a 3 column grid using Recyclerview:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val recyclerView = findViewById<RecyclerView>(R.id.recycler_view);
recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.layoutManager = GridLayoutManager(this, 3);
recyclerView.adapter = RecyclerAdapter()
}
data class Response(val movieTitle: String,
val moviePoster: Int,
val overview: String,
val ratings: Int,
val releaseDate: String,
val review: String)
}
JSONObjectin Java. In the Android SDK, there is aJSONObjectclass, but with respect to the language,JSONObjectis coming from a third-party library. I do not know what issues you have with third-party libraries and whether this means that you can no longer useJSONObject.without a 3rd party parser?by writing your own parser, I'd guess? But usually people don't because it's tricky and other people have already done it