N
N
Neonoviiwolf2019-09-08 11:24:53
Android
Neonoviiwolf, 2019-09-08 11:24:53

How to parse such a piece of Json?

Good, I can not understand how to parse this piece correctly

[
  {
    "id": 14256,
    "coord": {
      "lon": 48.570728,
      "lat": 34.790878
    },
    "country": "IR",
    "geoname": {
      "cl": "P",
      "code": "PPL",
      "parent": 132142
    },
    "langs": [
      {
        "de": "Azad Shahr"
      },
      {
        "fa": "آزادشهر"
      }
    ],
    "name": "Azadshahr",
    "stat": {
      "level": 1.0,
      "population": 514102
    },
    "stations": [
      {
        "id": 7030,
        "dist": 9,
        "kf": 1
      }
    ],
    "zoom": 10
  },
  {
    "id": 18918,
    "coord": {
      "lon": 34.058331,
      "lat": 35.012501
    },
    "country": "CY",
    "geoname": {
      "cl": "P",
      "code": "PPL",
      "parent": 146615
    },
    "langs": [
      {
        "en": "Protaras"
      },
      {
        "ru": "Протарас"
      }
    ],
    "name": "Protaras",
    "stat": {
      "level": 1.0,
      "population": 20230
    },
    "stations": [
      {
        "id": 5448,
        "dist": 42,
        "kf": 1
      }
    ],
    "zoom": 6
  }
]

A piece with an array of "langs" - I need to get the current en and ru data, if any. But each "langs" has an unknown number.
Here's what's there:
public ParserJson(CallbackParserData callback) {
        MainApp.app().appComponent().inject(this);
        String strJson;
        AssetManager assetManager = context.getAssets();
        try {
            InputStream stream = assetManager.open("city.json");
            int size = stream.available();
            byte[] buffer = new byte[size];
            stream.read(buffer);
            stream.close();
            strJson = new String(buffer, "UTF-8");

            JsonParser parser = new JsonParser();

            Object obj = parser.parse(strJson);
            JsonArray jsonArray = (JsonArray) obj;
            List<ParserWeather> weatherList = new ArrayList<>();

            for (int i = 0; jsonArray.size() > i; ++i)
                weatherList.add(new Gson().fromJson(jsonArray.get(i), ParserWeather.class));
            callback.returnListTempWeather(weatherList);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

General
@Getter
@Setter
public class ParserWeather {
    @SerializedName("id")
    private long id;

    @SerializedName("name")
    private String nameCity;

    @SerializedName("country")
    private String country;

    @SerializedName("langs")
    private List<ParserLangs> langs;
}

and here I don't know what to write
@Getter
@Setter
public class ParserLangs {
    @SerializedName("ru")
    private LangsRU langsRU;
    @SerializedName("en")
    private LangsEN langsEN;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2019-09-08
@Neonoviiwolf

Strange structure. Parse in List<Map<String, String>>, and if there is an opportunity to change the structure, change it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question