Tariq Ebadi
08/10/2023, 4:51 PMclass DataReceiver : Service() {
//other service code
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
val extra = intent.extras?.getString("extra")
// extra = {amount=1234, type=string1, id=string2}
return super.onStartCommand(intent, flags, startId)
}
}
//this is kotlin
Mohammadreza Khahani
08/11/2023, 6:36 AMdata class
corresponding to the JSON string, and then convert it to an instance of the data class
by using a popular JSON converter such as GSON.
If you need more info let me know to help you. 🙂
https://stackoverflow.com/questions/44117970/kotlin-data-class-from-json-using-gsonTariq Ebadi
08/11/2023, 7:12 PMval extra = intent.extras?.getString("extra")
val jsonObject = JSONObject(extra)
thats it haha.
Then its super easy to map each item.
val id = jsonObject.getString("id")
Or even add each one to a data class. No need to for gson. Because extras does not return a json object.
I think you’ve made the mistake of assuming the object at hand is a json object, it is just a string.
And this is from a service, so moving data is somewhat more complicated.
The correct approach is
service->repository(singleton)->viewModelMohammadreza Khahani
08/12/2023, 6:35 AMJSONObject
satisfied the requirement for the task. 👍
I understand your requirement because GSON can parse JSON string to an instance of the corresponding class automatically, and vice versa. 😉
You could take a look at the pros and cons of JSONObject
vs GSON library when you have some time. 🦾
https://stackoverflow.com/questions/42641301/org-json-jsonobject-vs-gson-library-jsonobjectGaurav Prakash
09/05/2023, 2:35 AM