1

I have a JSON string of this format

{
  "user": "sam",
  "password": "abcd1234",
  "categories":
    [
      {
        "fruit name": "watermelon"
      },
      {
        "fruit name":"jackfruit"
      },
      {
        "fruit name": "kiwi"
      }
    ],
  "store":"SPROUTS"
}

I was thinking I create a class of Structure like this

class Structure {
  String user;
  String password;
  String store;

  private Structure() {
    this.user = "sam";
    this.password = "abcd1234";
    this.store = "SPROUTS";
  }
}

And to parse the JSON, I can simply do it through Moshi through the following lines of code:

Moshi moshi = new Moshi.Builder().build();
Structure structure = new Structure();
String json = moshi.adapter(Structure.class).indent(" ").toJson(structure);

But, I also want to pass categories from my given JSON into this. How do I use categories with the same code? Also, what modifications are needed from my class structure?

1 Answer 1

1

Use a Java class to represent the category JSON object, just like you did for the Structure JSON object.

public final class Structure {
  public final String user;
  public final String password;
  public final String store;
  public final List<Category> categories;

  Structure(String user, String password, String store, List<Category> categories) {
    this.user = user;
    this.password = password;
    this.store = store;
    this.categories = categories;
  }

  public static final class Category {
    @Json(name = "fruit name") public final String fruitName;

    Category(String fruitName) {
      this.fruitName = fruitName;
    }
  }

  public static void main(String[] args) throws Exception {
    Moshi moshi = new Moshi.Builder().build();
    JsonAdapter<Structure> adapter = moshi.adapter(Structure.class);
    String json = "{\n"
        + "  \"user\": \"sam\",\n"
        + "  \"password\": \"abcd1234\",\n"
        + "  \"categories\":\n"
        + "    [\n"
        + "      {\n"
        + "        \"fruit name\": \"watermelon\"\n"
        + "      },\n"
        + "      {\n"
        + "        \"fruit name\":\"jackfruit\"\n"
        + "      },\n"
        + "      {\n"
        + "        \"fruit name\": \"kiwi\"\n"
        + "      }\n"
        + "    ],\n"
        + "  \"store\":\"SPROUTS\"\n"
        + "}";
    Structure value = adapter.fromJson(json);
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much @Eric Cochran .I actually did something similar myself.Forgot to update the comment here :).
class Structure { String user; String password; String store; List<String> Categories; private Structure() { this.user = "sam"; this.password = "abcd1234"; this.store = "SPROUTS"; this.categories = new Categories("banana") } private Class Categories{ String fruitName; private Categories(String fruit) { fruit name = fruit;}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.