Categories: java, android, json, kotlin, retrofit

Retrofit sending Pojo as form data

1 answer

Are there any build in way to convert java/kotlin pojo to Form data object. For example if i have two classes

data class Student(         var id: Int,         var name: String,         var age: Int )  data class College     (         var id: Int,         var address: String,         var students: Array<Student> ) 

and array of this objects would be like this in JSON

[   {     "id": 1,     "address": "12 Lincoln Street",     "students":      [       {         "id": 1,         "name": "Mike",         "age": 17       },       {         "id": 2,         "name": "Ann",         "age": 18       },       {         "id": 2,         "name": "Bob",         "age": 20       }     ]   },   {     "id": 2,     "address": "544 Peninsula Street Brooklyn",     "students":      [       {         "id": 4,         "name": "Tom",         "age": 17       },       {         "id": 5,         "name": "Liza",         "age": 21       },       {         "id": 6,         "name": "Rachel",         "age": 22       }     ]   } ] 

Can i somehow covert it automatically to Form Data to something like this?

college[0][id] = 1 college[0][address] = "12 Lincoln Street" college[0][students][0][id] = 1 college[0][students][0][name] = "Mike" college[0][students][0][age] = 17 college[0][students][1][id] = 2 college[0][students][1][name] = "Ann" college[0][students][1][age] = 18 college[0][students][2][id] = 3 college[0][students][2][name] = "Bob" college[0][students][2][age] = 20 college[1][id] = 2 college[1][address] = "544 Peninsula Street Brooklyn" college[1][students][0][id] = 4 college[1][students][0][name] = "Tom" college[1][students][0][age] = 17 college[1][students][1][id] = 5 college[1][students][1][name] = "Liza" college[1][students][1][age] = 21 college[1][students][2][id] = 3 college[1][students][2][name] = "Rachel" college[1][students][2][age] = 22 

Maybe there are some java/kotlin libraries that can convert Json string to form data?

All answers to this question, which has the identifier 61238041

The best answer:

You can create 2 model (pojo class):

data class Student(     @Expose     @SerializedName("id")     var id: Int,     @Expose     @SerializedName("name")     var name: String,     @Expose     @SerializedName("age")     var age: Int )  data class College(     @Expose     @SerializedName("id")     var id: Int,     @Expose     @SerializedName("address")     var address: String,     @Expose     @SerializedName("students")     var students: List<Student> ) 

Then parse from json to object like that:

gson.fromJson(jsonString,  // or  gson.fromJson(jsonString, 

Hope this help!

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type is referenced as an interface from”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data