Kotlin - work with JSON

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@phash·
0.000 HBD
Kotlin - work with JSON
The world is full of RESt-JSON Endpoints. What a shame not to use them!

To increase the usefulness of JSON I advice you to use the [Json to Kotlin Class Plugin](https://plugins.jetbrains.com/plugin/9960-json-to-kotlin-class-jsontokotlinclass-) for IntelliJ IDEA/ Android Studio. Just add it to your IDE and use it! 

# how to use 
Get a JSON file, and copy the contents. 
```Kotlin
{
        "title": "Sample",
        "name": "main_sample",
        "width": 500,
        "height": 500
    }
```
Right click on your project / package and use "Create Data Class from JSON"
enter a name, and post the code from above.
This will create a new kotlin data class!
```Kotlin
{
data class Item(
        var title: String,
        var name: String,
        var width: Int,
        var height: Int
)
    }
```
That's it!
You can now use 
```Kotlin
var json = URL("www.mywebservice.com/getData").readText()
var item = Gson().fromJson(json, Item::class.java)
println(item.title)
...
```
smooth, isn't it?
👍 , ,