Problem when parsing json

58 views
Skip to first unread message

Abdoul'Anzize A B S BACHIR

unread,
Jan 9, 2020, 4:48:12 PM1/9/20
to google-gson
Dear,

The question may have already been asked or perhaps never because it is too zero. I therefore apologize in advance. With GSON that I just discovered, in my java application, I try to get data from a json request. I have the following result from the json request:

{"symbol":"MSFT","stock_exchange_short":"NASDAQ","timezone_name":"America/New_York","intraday":{"2020-01-09 15:42:56":{"open":"161.65","close":"161.65","high":"161.65","low":"161.65","volume":"41201"},
"2020-01-09 15:40:00":{"open":"161.60","close":"161.60","high":"161.62","low":"161.57","volume":"41201"},
"2020-01-09 15:35:00":{"open":"161.56","close":"161.59","high":"161.63","low":"161.50","volume":"140067"},
"2020-01-09 15:30:00":{"open":"161.57","close":"161.55","high":"161.64","low":"161.54","volume":"193546"}}


How can I retrieve the open, close, high, low, volume information for each period in order to insert it into a database ?

I tried this : 
JsonElement data = jp.parse(new InputStreamReader((InputStream) request.getContent()));
JsonObject rootobj = data.getAsJsonObject(); //May be an array, may be an object. 
JsonObject intradayObject = rootobj.getAsJsonObject("intraday");

The result is
{"2020-01-09 15:42:56":{"open":"161.65","close":"161.65","high":"161.65","low":"161.65","volume":"41201"},
"2020-01-09 15:40:00":{"open":"161.60","close":"161.60","high":"161.62","low":"161.57","volume":"41201"},
"2020-01-09 15:35:00":{"open":"161.56","close":"161.59","high":"161.63","low":"161.50","volume":"140067"},
"2020-01-09 15:30:00":{"open":"161.57","close":"161.55","high":"161.64","low":"161.54","volume":"193546"}}

But I am unable to continue as I am trying to transform the intradayObject to a JsonArray (I have an error message).

Thanks a lot for your help!


Christopher Tubbs

unread,
Jan 9, 2020, 11:13:39 PM1/9/20
to google-gson
JSON arrays use square brackets. You can't tell GSON to parse as an array something which is not a valid JSON array.
If you need the data in an array, you'll need to iterate over `intradayObject.entrySet()` yourself and put the contents in an array yourself.

Abdoul'Anzize A B S BACHIR

unread,
Jan 21, 2020, 7:05:13 PM1/21/20
to google-gson
Many thanks for your reply,

I am must be a zero and I am unable to reach my result with intradayObject.entrySet().

It is quite difficult for me. 

Regards

Christopher Tubbs

unread,
Jan 22, 2020, 2:14:28 AM1/22/20
to google-gson
Here's a very rough idea of what I was talking about with entrySet():

    String jsonStr =
        "{\"symbol\":\"MSFT\",\"stock_exchange_short\":\"NASDAQ\",\"timezone_name\":\"America/New_York\",\"intraday\":{\"2020-01-09 15:42:56\":{\"open\":\"161.65\",\"close\":\"161.65\",\"high\":\"161.65\",\"low\":\"161.65\",\"volume\":\"41201\"},\n"
            + "\"2020-01-09 15:40:00\":{\"open\":\"161.60\",\"close\":\"161.60\",\"high\":\"161.62\",\"low\":\"161.57\",\"volume\":\"41201\"},\n"
            + "\"2020-01-09 15:35:00\":{\"open\":\"161.56\",\"close\":\"161.59\",\"high\":\"161.63\",\"low\":\"161.50\",\"volume\":\"140067\"},\n"
            + "\"2020-01-09 15:30:00\":{\"open\":\"161.57\",\"close\":\"161.55\",\"high\":\"161.64\",\"low\":\"161.54\",\"volume\":\"193546\"}}}";
    var gson = new Gson();

    var jsonObj = gson.fromJson(jsonStr, JsonObject.class);
    var intraday = jsonObj.getAsJsonObject("intraday");
    intraday.entrySet().forEach(e -> {
      var stats = gson.fromJson(e.getValue(), JsonObject.class);
      var sb = new StringBuilder(e.getKey());
      sb.append(String.format("%n    open %.2f%n", stats.get("open").getAsDouble()));
      sb.append(String.format("   close %.2f%n", stats.get("close").getAsDouble()));
      sb.append(String.format("    high %.2f%n", stats.get("high").getAsDouble()));
      sb.append(String.format("     low %.2f%n", stats.get("low").getAsDouble()));
      sb.append(String.format("  volume %d%n", stats.get("volume").getAsLong()));
      System.out.print(sb.toString());
    });

Abdoul'Anzize A B S BACHIR

unread,
Jan 23, 2020, 2:34:35 PM1/23/20
to google-gson
Many thanks Christopher ! It works !
Reply all
Reply to author
Forward
0 new messages