0

I have a problem.

Earlier, I parsed multiple objects from 1 single JSON to an ArrayList<Object>. This worked great, but now I need to do something different and I have no idea how to this. I have the following JSON:

{
   "Market":"USDT",
   "Coin":"BTC",
   "Candles":{
      "USDT":{
         "BTC":{
            "3h":[
               {
                  "Close":"used"
               }
            ],
            "1h":[
               {
                  "EMA20":"used",
                  "EMA200":"used"
               }
            ],
            "5m":[
               {
                  "EMA5":"used",
                  "EMA20":"used"
               },
               {
                  "EMA5":"used",
                  "EMA20":"used"
               }
            ]
         }
      }
   }
}

From this JSON I want to get an ArrayList<String> with only the values: "3h", "1h", "5m". The values inside the array don't matter for me, I just need those 3 periods. Here is the json parser I used for parsing something to a class:

public ArrayList<Agent> parseJsonToList(String StrJSON) {

    Gson gson = new GsonBuilder().registerTypeAdapter(LocalDateTime.class, (JsonDeserializer<LocalDateTime>) (json, type, jsonDeserializationContext) -> {
        try{
            return LocalDateTime.parse(json.getAsJsonPrimitive().getAsString(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        } catch (DateTimeParseException e){
            return LocalDateTime.parse(json.getAsJsonPrimitive().getAsString(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS"));
        }
    }).create();

    Type listType = new TypeToken<ArrayList<Agent>>() {}.getType();

    return gson.fromJson(StrJSON, listType);

}

Now how can I get those 3 values from my new JSON, without creating Classes (if possible)?

Hakan Dilek
  • 2,178
  • 2
  • 23
  • 35
A. Vreeswijk
  • 822
  • 1
  • 19
  • 57
  • Does this answer your question? [JSON parsing using Gson for Java](https://stackoverflow.com/questions/5490789/json-parsing-using-gson-for-java) – maio290 Jan 29 '21 at 13:43
  • Kind of.... How do I get the name of the object, because I don't want the content? – A. Vreeswijk Jan 29 '21 at 13:45
  • You get the root object, then you get the Candels object, thann the USDT object, than the BTC object and then you can get an entrySet and check if the type is JsonArray. Or you should just an ugly reegex and substring, would do the job too, but yeah. – maio290 Jan 29 '21 at 13:47

1 Answers1

0

You can use this ugly code for your purposes, but this code is just to show how you can achieve that and I don't recommend use this code in production:

private List<String> extractPeriods(String jsonString) {
    Gson gson = new Gson();
    Map map = gson.fromJson(jsonString, Map.class);

    Map candlesMap = (Map) map.get("Candles");
    if (Objects.isNull(candlesMap))
        return null;
    Map usdtMap = (Map) candlesMap.get("USDT");
    if (Objects.isNull(usdtMap))
        return null;
    Map btcMap = (Map) usdtMap.get("BTC");
    if (Objects.isNull(btcMap))
        return null;
    return new ArrayList<>(btcMap.keySet());
}

You can also write regexp to search all this values, of course.
Or you can create objects only partially. For example you can create objects for root, Candles, USDT, and for BTC field just use a Map type.

Naitonium
  • 89
  • 1
  • 6