Good day. I want to parse this json file.

The code itself for this case:

import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import org.apache.http.ParseException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.FileReader; public class JSONtest { public static void main(String args[]) throws Exception { JSONtest jsonTest = new JSONtest(); Object ads[] = jsonTest.getAd(); Integer noneAds = 0; for (int i = 1; i <ads.length; i++) { if (ads[i].equals(noneAds)) { System.out.println("Bingo"); } } } public Object[] getAd() throws Exception { BufferedReader bufferedReader = new BufferedReader(new FileReader("C:/TAP/src/main/resources/vk.json")); JsonParser parser = new JsonParser(); Object [] ads = new Object[7]; try { JsonObject jsonResp = (JsonObject) parser.parse(bufferedReader); JsonArray postsList = (JsonArray) jsonResp.get("response"); JsonObject unicPost = null; JSONObject jsonObject; for (int i = 1; i < postsList.size(); i++) { unicPost = (JsonObject) postsList.get(i); ads[i] = unicPost.get("marked_as_ads"); System.out.println("Ads from sub " + ads[i]); } } catch (ParseException e) {e.printStackTrace();} return ads; } } 

In main'e I want to compare the data obtained from the getAds method with the value 0 of type Integer, but since they are of a different type, the result is false, where true should be; I tried to build, but the ClassCastException always crashed. What can be done?

enter image description here

    3 answers 3

    Option 1

    You use the public JsonElement get(String memberName) method public JsonElement get(String memberName) .

    You have everything correctly written, but it is more correct to save the values ​​in JsonElement when reading, or rather in the JsonElement[] array JsonElement[] That is, like this:

     JsonElement[] ads = new JsonElement[7]; 

    Then the reading in the cycle will remain the same as yours. Next, you can convert the JsonElement[] json array using the JsonElement.getAsInt() method. That is so:

     int variable = ads[i].getAsInt(); 

    Option 2

    Or immediately save the values ​​to the array int[] ads via the .getAsInt() method. That is so:

     ... ads[i] = unicPost.get("marked_as_ads").getAsInt(); ... 

      Try the method:

       int getInt (String name) 

      instead:

       Object get (String name) 

      That is, it will be like this:

       ads[i] = unicPost.getInt("marked_as_ads"); 

      Accordingly, it is necessary to change the type of the array to int[] .

      Then you can work with primitives and compare them with the operator == .

        Why write to an array of JSON data, if you can turn them into a string. Try this:

         ads[i] = unicPost.get("marked_as_ads").getAsString(); 

        In this case, declare the array as String [].

        And then already, if you need to be sure to compare with Integer, you can apply Integer.parseInt (String s) to the string and get a number from the string.

        https://stackoverflow.com/questions/34120882/gson-jelement-getasstring-vs-jelement-tostring

        By the way, there is a getAsInt () method. So you can immediately get the number. Only in your file not all data is numeric. The string in this sense is somehow more universal.

        https://static.javadoc.io/com.google.code.gson/gson/2.6.2/com/google/gson/JsonElement.html