This article shows how to parse JSON using Moshi.
Table of contents:
- 1. Download Moshi
- 2. Parse JSON using Moshi
- 3. Parse JSON Array using Moshi
- 4. Convert Java Object to JSON String
- 5. Download Source Code
- 6. References
P.S Tested with Moshi 1.15.1
1. Download Moshi
Declare moshi
in the pom.xml
, and it will automatically pull in other necessary dependencies.
pom.xml
<dependency>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi</artifactId>
<version>1.15.1</version>
</dependency>
Terminal
$ mvn dependency:tree
[INFO] \- com.squareup.moshi:moshi:jar:1.15.1:compile
[INFO] +- com.squareup.okio:okio-jvm:jar:3.7.0:compile
[INFO] | \- org.jetbrains.kotlin:kotlin-stdlib:jar:1.9.21:compile
[INFO] | \- org.jetbrains:annotations:jar:13.0:compile
[INFO] \- org.jetbrains.kotlin:kotlin-stdlib-jdk8:jar:1.8.21:compile
[INFO] \- org.jetbrains.kotlin:kotlin-stdlib-jdk7:jar:1.8.21:compile
2. Parse JSON using Moshi
A simple JSON string.
{
"name": "mkyong",
"age": 20
}
The following example uses Moshi to convert a JSON string to a Java object Person
.
Person.java
package com.mkyong.json.model;
public class Person {
private String name;
private int age;
//getters, setters, constructors
}
JsonToObjectExample.java
package com.mkyong.json.moshi;
import com.mkyong.json.model.Person;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
public class JsonToObjectExample {
public static void main(String[] args) {
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<Person> jsonAdapter = moshi.adapter(Person.class);
String json = "{\"name\":\"mkyong\",\"age\":42}";
try {
// JSON to Java object
Person person = jsonAdapter.fromJson(json);
System.out.println(person);
} catch (Exception e) {
System.err.println("Error parsing JSON: " + e.getMessage());
}
}
}
Output
Person{name='mkyong', age=42}
3. Parse JSON Array using Moshi
Array of JSON.
[
{
"name": "mkyong",
"age": 42
},
{
"name": "ah pig",
"age": 20
}
]
The following example uses Moshi to convert an array of JSON objects to a list of Java object List<Person>
.
JsonArrayToObjectExample.java
package com.mkyong.json.moshi;
import com.mkyong.json.model.Person;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import com.squareup.moshi.Types;
import java.lang.reflect.Type;
import java.util.List;
public class JsonArrayToObjectExample {
public static void main(String[] args) {
Moshi moshi = new Moshi.Builder().build();
// List<Person>
Type type = Types.newParameterizedType(List.class, Person.class);
JsonAdapter<List<Person>> adapter = moshi.adapter(type);
// json array
String json = "[{\"name\":\"mkyong\", \"age\":42}, {\"name\":\"ah pig\", \"age\":20}]";
try {
// convert json array to list of objects
List<Person> list = adapter.fromJson(json);
if (list != null) {
for (Person person : list) {
System.out.println(person);
}
}
// convert list of objects to json array
// String json2 = adapter.toJson(list);
// Output: [{"age":42,"name":"mkyong"},{"age":20,"name":"ah pig"}]
// System.out.println(json2);
} catch (Exception e) {
System.err.println("Error parsing JSON: " + e.getMessage());
}
}
}
output
Person{name='mkyong', age=42}
Person{name='ah pig', age=20}
4. Convert Java Object to JSON String
The following example uses Moshi to convert a Java object Person
to a JSON String and print it to the console.
ObjectToJsonExample.java
package com.mkyong.json.moshi;
import com.mkyong.json.model.Person;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
public class ObjectToJsonExample {
public static void main(String[] args) {
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<Person> jsonAdapter = moshi.adapter(Person.class);
Person person = new Person("mkyong", 42);
try {
String json = jsonAdapter.toJson(person);
System.out.println(json);
} catch (Exception e) {
System.err.println("Error parsing JSON: " + e.getMessage());
}
}
}
Output
output.json
{"age":42,"name":"mkyong"}
5. Download Source Code
$ git clone https://github.com/mkyong/java-json
$ cd moshi
6. References
- Moshi Github
- Parse JSON string with Jackson
- Parse JSON Array with Jackson
- Convert Java Objects to JSON with Jackson
The post How to parse JSON using Moshi appeared first on Mkyong.com.