Skip to main content

Moshi Tutorial and Examples

Moshi is a JSON parsing library for Android. It is a lightweight and fast library that can parse JSON data into Java objects. Moshi is developed by Square, the same company that developed Retrofit and OkHttp. Moshi is an alternative to Gson, another popular JSON parsing library.

In this article, we'll learn how to use Moshi to parse JSON data in Android.

Step 1: Add Moshi Dependencies

To use Moshi in your Android project, you need to add the Moshi dependencies to your build.gradle file. Add the following code in your app-level build.gradle file:

dependencies {
implementation 'com.squareup.moshi:moshi:1.12.0'
kapt 'com.squareup.moshi:moshi-kotlin-codegen:1.12.0'
}

Step 2: Create a Data Model

Before parsing JSON data, you need to create a data model that represents the JSON data. For example, let's say we have the following JSON data:

{
"name": "John Doe",
"age": 30
}

We can create a data model for this JSON data as follows:

data class Person(val name: String, val age: Int)

Step 3: Parse JSON Data

To parse the JSON data into a Java object, we need to create a Moshi object and use it to create a JSON adapter. We can then use the JSON adapter to parse the JSON data. Here's an example:

val json = """
{
"name": "John Doe",
"age": 30
}
""".trimIndent()

val moshi = Moshi.Builder().build()
val adapter = moshi.adapter(Person::class.java)

val person = adapter.fromJson(json)

In the code above, we first create a Moshi object using the Moshi.Builder() method. We then create a JSON adapter using the adapter() method and pass in the data model class (Person in this case). Finally, we use the adapter to parse the JSON data using the fromJson() method.

Step 4: Generate JSON Data

To generate JSON data from a Java object, we can use the same adapter object we created in Step 3. Here's an example:

val person = Person("John Doe", 30)

val json = adapter.toJson(person)

In the code above, we create a Java object (person) and use the adapter object to generate JSON data using the toJson() method.

Moshi Hello World

Let's look at a hello world in moshi.

import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;

import java.io.IOException;

/**
* Moshi Hello World
*
*/
public class MoshiHelloWorld {
public static void main(String[] args) throws IOException {
// init class
Destination destination = new Destination();
destination.setName("Alpha Centauri");

Starcraft starcraft = new Starcraft();
starcraft.setName("Enterprise");
starcraft.setDestination(destination);

// convert to json
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<Starcraft> jsonAdapter = moshi.adapter(Starcraft.class);

String jsonString = jsonAdapter.toJson(starcraft);
System.out.println("json " + jsonString); //print "json {"name":"Enterprise","destination":{"name":"Alpha Centauri"}}"

// convert from json
Starcraft newStarcraft = jsonAdapter.fromJson(jsonString);
newStarcraft.show(); // print "Enterprise , Alpha Centauri!"
}

private static class Starcraft {
private String name;
private Destination destination;

String getName() {
return name;
}

void setName(String name) {
this.name = name;
}

Destination getDestination() {
return destination;
}

void setDestination(Destination destination) {
this.destination = destination;
}

void show() {
System.out.println();
System.out.println(getName() + " , " + getDestination().getName() + "!");
}
}

private static class Destination {
private String name;

String getName() {
return name;
}

void setName(String name) {
this.name = name;
}
}

}

Conclusion

Moshi is a powerful JSON parsing library for Android. It is easy to use and provides a fast and lightweight solution for parsing JSON data. In this article, we learned how to use Moshi to parse and generate JSON data in Android. We also learned how to create a data model for JSON data and use it to parse JSON data into Java objects.