0

Is there a cleaner way to get the value? I'm not a Kotlin expert and i can't seem to find alot on this online

var options = request.getJSONObject("optionsObject");
var readingPosition = options.getJSONObject("readingPosition");
var selector = readingPosition.getJSONObject("selector");
var value = selector.getString("value");

the json:

{
   "optionsObject":{
      "readingPosition":{
         "format":"application\/epub+zip",
         "selector":{
            "type":"FragmentSelector",
            "conformsTo":"http:\/\/www.idpf.org\/epub\/linking\/cfi\/epub-cfi.html",
            "value":"epubcfi(\/6\/2!\/0)"
         }
      },
      "readingProgress":0
   }
}
DennisVA
  • 2,068
  • 1
  • 25
  • 35

1 Answers1

0

if you are using retrofit just make a class

data class youcalssname(
 val yourJsonobjet:YourJsonobjectType,
 //like this
 val value:String,
 )

and get the data easily yourclassname.value for more check this

Amit pandey
  • 1,149
  • 1
  • 4
  • 15
  • idk feels like overkill to me – DennisVA Mar 19 '20 at 11:26
  • 2
    for this kind of json response i think your code is clean and less complex. – Amit pandey Mar 19 '20 at 11:33
  • i agree, but it still suprises me that there's not a more efficient way – DennisVA Mar 19 '20 at 11:52
  • if you are getting this type of response in lots of api than you can create a pojo class or model and get and set the data into model class and get it but if its seem you getting only one time response thats okay its clean and less complex – Amit pandey Mar 19 '20 at 12:54
  • if you get the response one time its okay but if you get same response in different api and in different activity and fragment you have to write all you code agagin to get the data but if you use model you have just pass the model and get responce into model and no need to call request.getJSONObject("optionsObject"); . and you can simple pass the model from one activity to another whole data it reduce code complex and if from server the api responce object change you have to change every where but if you use model you just need to change only in model . – Amit pandey Mar 19 '20 at 13:11