3

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)
}
3
  • 1
    "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." -- there is no JSONObject in Java. In the Android SDK, there is a JSONObject class, but with respect to the language, JSONObject is 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 use JSONObject. Commented Apr 15, 2018 at 20:54
  • I apologize for the poor wording. You're right. I am more comfortable parsing the JSON manually, instead of using a parser like GSON or Volley. That's what I meant. Commented Apr 15, 2018 at 20:55
  • 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 Commented Apr 15, 2018 at 20:56

2 Answers 2

3

I am more comfortable parsing the JSON manually, instead of using a parser like GSON or Volley.

I am interpreting this as meaning that you want to pull data out of the JSON, walking the JSON structure yourself, rather than using a JSON parser that populates model objects.

In that case, there is nothing stopping you from using JSONObject in Kotlin, just as you are using Bundle and LinearLayoutManager and RecyclerView in Kotlin. So, val json = JSONObject(jsonText) and stuff should work just fine.

Sign up to request clarification or add additional context in comments.

2 Comments

Could you give an example? Say that you had to get the posters like I am doing. Would you do that in onCreate? and does jsonText, in your example, mean the API or an object from the API?
@OnurOzbek: "Could you give an example?" -- I do not use JSONObject much and have never used it in Kotlin, so I don't have any code lying around to point you towards. "Would you do that in onCreate?" -- you would parse the JSON as part of your Web service request. Where that Web service request is made is up to you, though triggering it in onCreate() is a common choice. Just remember configuration changes and background threads. "and does jsonText, in your example, mean the API or an object from the API?" -- it means the raw JSON text that you got from the Web service (e.g., via OkHttp3).
1

Exactly the same way like in Java. Code written in Java can run in Kotlin applications. Remember also that Java doesn't support JSON by native and this JSONObject you mentioned is from com.google.gson package.

7 Comments

I know but is it possible to write the entire thing only in Kotlin?
@EpicPandaForce and that's what the question is about. How do I do that?
Same way you would in Java. Just in Kotlin.
Okay so can you give me a code block as an example?
You wanted an example. In those repo you have much more than examples. Haven't you noticed it has open source code? XD
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.