jsonHow to use Moshi to parse a JSON string and convert it...

How to use Moshi to parse a JSON string and convert it into a Java object?

Moshi is a modern JSON library for Android developed by Square. It is a compact and efficient library that makes it easy to work with JSON in Android applications.

Moshi provides a set of classes for data binding, which allow you to convert JSON to and from Java objects. It also provides classes for streaming JSON, for example for parsing large amounts of data or for generating JSON data on the fly.

Moshi has a number of advanced features, including support for custom serialization and deserialization with custom converters, support for JSON encoding and decoding with custom adapters, and the ability to customize the parsing and generation process. It is widely used in Android applications for working with JSON data.

<!-- https://mvnrepository.com/artifact/com.squareup.moshi/moshi -->
<dependency>
    <groupId>com.squareup.moshi</groupId>
    <artifactId>moshi</artifactId>
    <version>1.14.0</version>
    <scope>runtime</scope>
</dependency>

Here is an example of how to use Moshi to parse a JSON string and convert it into a Java object:

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

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

public class MoshiExample {
    public static void main(String[] args) throws IOException {
        String json = "[{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}, {\"name\":\"Jane\",\"age\":25,\"city\":\"Chicago\"}]";

        Moshi moshi = new Moshi.Builder().build();
        Type type = Types.newParameterizedType(List.class, Person.class);
        JsonAdapter<List<Person>> adapter = moshi.adapter(type);

        List<Person> people = adapter.fromJson(json);

        for (Person person : people) {
            System.out.println(person.name);
            System.out.println(person.age);
            System.out.println(person.city);
        }
    }
}

class Person {
    String name;
    int age;
    String city;
}

To generate a JSON string from a Java object, you can use the toJson method of the JsonAdapter class:

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

public class MoshiExample {
    public static void main(String[] args) throws IOException {
        Person person = new Person("John", 30, "New York");

        Moshi moshi = new Moshi.Builder().build();
        JsonAdapter<Person> adapter = moshi.adapter(Person.class);

        String json = adapter.toJson(person);

        System.out.println(json);  // Outputs "{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}"
    }
}

class Person {
    String name;
    int age;
    String city;

    public Person(String name, int age, String city) {
        this.name = name;
        this.age = age;
        this.city = city;
    }
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe Today

GET EXCLUSIVE FULL ACCESS TO PREMIUM CONTENT

[tds_leads input_placeholder="Your email address" btn_horiz_align="content-horiz-center" pp_msg="SSd2ZSUyMHJlYWQlMjBhbmQlMjBhY2NlcHQlMjB0aGUlMjAlM0NhJTIwaHJlZiUzRCUyMiUyMyUyMiUzRVByaXZhY3klMjBQb2xpY3klM0MlMkZhJTNFLg==" pp_checkbox="yes" tdc_css="eyJhbGwiOnsibWFyZ2luLXRvcCI6IjMwIiwibWFyZ2luLWJvdHRvbSI6IjMwIiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tdG9wIjoiMjAiLCJtYXJnaW4tYm90dG9tIjoiMjAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" display="column" gap="eyJhbGwiOiIyMCIsInBvcnRyYWl0IjoiMTAifQ==" f_msg_font_family="702" f_input_font_family="702" f_btn_font_family="702" f_pp_font_family="789" f_pp_font_size="eyJhbGwiOiIxNCIsInBvcnRyYWl0IjoiMTIifQ==" f_btn_font_spacing="1" f_btn_font_weight="600" f_btn_font_size="eyJhbGwiOiIxNiIsImxhbmRzY2FwZSI6IjE0IiwicG9ydHJhaXQiOiIxMyJ9" f_btn_font_transform="uppercase" btn_text="Subscribe Today" btn_bg="#000000" btn_padd="eyJhbGwiOiIxOCIsImxhbmRzY2FwZSI6IjE0IiwicG9ydHJhaXQiOiIxNCJ9" input_padd="eyJhbGwiOiIxNSIsImxhbmRzY2FwZSI6IjEyIiwicG9ydHJhaXQiOiIxMCJ9" pp_check_color_a="#000000" f_pp_font_weight="500" pp_check_square="#000000" msg_composer="" pp_check_color="rgba(0,0,0,0.56)"]

Get unlimited access to our EXCLUSIVE Content and our archive of subscriber stories.

Latest article

Exit mobile version