I am making a project to get an API using Retrofit, in order to get the data from the website, I have to understand that there is some data stored into multiple data within the JSON, such as this:
launches [{
id* string
provider string
}]
What I want from the JSON code is to convert it into a Java/Kotlin code for the small project, If someone can able to explain to me OR show it to me in a Java/Kotlin code on how to make the setter/getter sequence on this code. Thank you for your time.
Here is the full example JSON format:
Article{
id integer
featured boolean
default: false
title string
url string
imageUrl string
newsSite string
summary string
publishedAt string
launches [{
id* string
provider string
}]
events [{
id* string
provider string
}]
}
This one actually json :
{
"employee":{"name":"John", "age":30, "city":"New York"}
}
CodePudding user response:
What you are looking for is JSON Parsing Libraries or JSON Converters for Retrofit.
You see Retrofit does not have a built-in JSON converter to parse from JSON string to Java or Kotlin objects. Instead, Retrofit ships support for Moshi
, Jackson
or GSON
etc.
Converters like Moshi are JSON parsers that convert a JSON string into Kotlin objects or vice-versa.
These converters are specified on the Retrofit.Builder
. You also need to add the converters' dependencies alongside that of Retrofit to your project.
/*using MoshiBuilder to create MoshiObject i.e. the
JSON-String-to-Kotlin-Object converter*/
/*For Moshi's annotations to work properly with Kotlin
add the KotlinJsonAdapterFactory*/
private val moshi = Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
//using Retrofit Builder to create Retrofit Object
private val retrofit = Retrofit.Builder()
.addConverterFactory(MoshiConverterFactory.create(moshi)) //handles JSON
.baseUrl(BASE_URL)
.build()
There is a plugin called JsonToKotlinClass which you can use to copy the JSON string from your REST API and paste it on the plugin to generate a Kotlin Object for you.
I would advise you to watch video courses on how to go about the whole thing. For Example watch this course.
CodePudding user response:
Java and Kotlin can use Google's GSON library that lets you convert Plain Old Java Objects (POJOs) to and from a JSON string. Also, as someone pointed out, what you have posted looks like incorrect syntax for the JSON schema. Please make sure it adheres to the schema. First, add the GSON dependency in your pom.xml (or its equivalent in Kotlin, if different. Sorry, I'm a Java programmer).
Dependency:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
This is how you can use GSON -
Create an Artifact class in Java/Kotlin. This class will be your blueprint for creating objects.
Instantiate a GSON object in the class where you have the JSON string.
Gson gson = new Gson();
- Instantiate a new object and map the JSON string to POJO using GSON.
String exampleJson = "{"Article":{ "id":123, "featured": true, etc... }}";
Article myArticleObj = gson.fromJson(exampleJson, Article.class);
You can use gson.toJson(Object) for the reverse conversion to a JSON string.
And that's it! Hope it helped!