https://kotlinlang.org logo
#android
Title
# android
n

Nikolay Puliaev

05/19/2020, 12:59 PM
Hi, guys. Is there a way to merge two org.json.JSONObject using Kotlin? I’ve already googled it, but it was unsuccessfully. Maybe someone has an approach for that? Thanks in advance.
d

Dominaezzz

05/19/2020, 1:01 PM
Merge how? How do you want to handle duplicate keys?
n

Nikolay Puliaev

05/19/2020, 1:04 PM
I have predefined keys for my JSON, and they will not duplicate. For example:
Copy code
Json1
{
	"id" : "1",
	"name" : "Example"
}
Json2
{
	"age": "99"
}

Result Json should look like
{
	"id" : "1",
	"name" : "Example",
	"age": "99"
}
s

Saurabh

05/19/2020, 1:10 PM
If you have predefined keys like you mentioned, you can just iterate over the second
JSONObject
and put the values in the first
JSONObject
☝🏼 2
n

Nikolay Puliaev

05/19/2020, 1:14 PM
Yeah, it’s the first what I was thinking about. I thought maybe there can be more elegant method. Alright, thanks. I will try that one.
477 Views