0

I have this Json File:

{
  "code": "#0",
  "data": {
    "list": [
      "Chris",
      "Matt"
    ],
    "time": 1453318488122
  },
  "millis": "1453492643260"
}

In Java using Gson how do I access the "data" part, then "list" and turn it into an ArrayList? I have tried looking online and they are all showing things way too complicated for what I want, like serializing a class into Json. I don't want that.. How do I access "List" array inside of "data" object and put into an ArrayList?

2 Answers 2

1
JsonObject fullObject = new JsonParser().parse(yourFileData).getAsJsonObject();
JsonObject data = fullObject.getAsJsonObject('data');
JsonArray dataList = data.getAsJsonArray('list');

From there you can use dataList and iterate the elements.

Sign up to request clarification or add additional context in comments.

1 Comment

Than-yo so much! Worked perfect! I'm sure I tried this but idk :D. Btw ' doesn't work in java, you mean " :D
0

You can serialize/deserialize such JSON without any additional adapter classes if you define a class like this one.

class MyPojo {
  String code;
  long millis;
  Data data;

  static class Data {
    List<String> list;
    long time;
  }
}

(+ eventually @SerializedName annotations if your strategy requires them)

2 Comments

I don't get it? Isn't there a simple way I can just take that JsonArray List and turn it into an ArrayList right there? I'm sure there is... I just don't know how to do it.
If all you need is to extract List from a json text then just take a look at @NG's answer.

Your Answer

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