Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a JSONObject from String in Kotlin?

Tags:

json

kotlin

I need to convert a string {\"name\":\"test name\", \"age\":25} to a JSONObject

like image 472
nkukday Avatar asked May 31 '17 22:05

nkukday


People also ask

How do I create a JSON object?

String message; JSONObject json = new JSONObject(); json. put("test1", "value1"); JSONObject jsonObj = new JSONObject(); jsonObj. put("id", 0); jsonObj. put("name", "testName"); json.


2 Answers

Perhaps I'm misunderstanding the question but it sounds like you are already using org.json which begs the question about why

val answer = JSONObject("""{"name":"test name", "age":25}""") 

wouldn't be the best way to do it? What was wrong with the built in functionality of JSONObject?

like image 181
Ryba Avatar answered Oct 04 '22 12:10

Ryba


val rootObject= JSONObject() rootObject.put("name","test name") rootObject.put("age","25") 
like image 31
arjun shrestha Avatar answered Oct 04 '22 12:10

arjun shrestha