0

I'd like to know how to parse a json object which will be uploaded to my server to retrieve the admob ad IDs from it.

Example:

{
    "response":{
                "Interstial AD":"ca-xxxxxxxxxxx"
              }
}

will be sent to

mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("JsonDATA");
mInterstitialAd.loadAd(new AdRequest.Builder().build());

mInterstitialAd.setAdListener(new AdListener(){

    @Override
    public void onAdLoaded() {
        super.onAdLoaded();
        mInterstitialAd.show();
    }
}

Any help would be appreciated!

EDIT:

Tired this:

  String jsonToProcess = "https://drive.google.com/uc?id=113RUepiYecy5pBwj-t4BtBXwlQwgf-dU";
        String interstialAd = new JsonParser().parse(jsonToProcess).getAsJsonObject()
                .get("response").getAsJsonObject()
                .get("Interstial AD").getAsString();

        if (getResources().getString(R.string.admob_interstitial_id).length() > 0
                && Config.INTERSTITIAL_INTERVAL > 0
                && !SettingsFragment.getIsPurchased(this)) {
            mInterstitialAd = new InterstitialAd(this);
            mInterstitialAd.setAdUnitId(interstialAd);
            AdRequest adRequestInter = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
            mInterstitialAd.loadAd(adRequestInter);

            mInterstitialAd.setAdListener(new AdListener() {
                @Override
                public void onAdClosed() {
                    // Load the next interstitial.
                    mInterstitialAd.loadAd(new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build());
                }

            });
        }

My Json file:

{
    "response": [
        {
            "Interstial AD": "ca-app-pub-3940256099942544/1033173712"
        },
        {
            "Banner AD": "ca-app-pub-3940256099942544/6300978111"
        }
  ]
}
Steven Rõgrê
  • 165
  • 1
  • 13
  • If you just want to get data from josn,you can use some jar for parsing json such as `fastjson,gson` and then you can get data easily. – Mr.DW Jul 12 '19 at 01:52

1 Answers1

2

Using Gson it's pretty easy to do this...

Option 1:

Edit per question edit:

String jsonToProcess = "your json string here"  
JsonElement jsonElement = new JsonParser().parse(json);

String iterstialAd = null;
String bannerAd = null;

for (JsonElement obj : jsonElement.getAsJsonObject().get("response").getAsJsonArray()) {
    if (obj.getAsJsonObject().get("Interstial AD") != null) {
        iterstialAd = obj.getAsJsonObject().get("Interstial AD").getAsString();
        continue;
    }
    if (obj.getAsJsonObject().get("Banner AD") != null) {
        bannerAd = obj.getAsJsonObject().get("Banner AD").getAsString();
        continue;
    }
}

Option 2:

Create response model and deserialize your response...

import com.google.gson.annotations.SerializedName;

public class ResponseModel {

    private Response response;

    public Response getResponse() {
        return response;
    }

    public void setResponse(Response response) {
        this.response = response;
    }

    public static class Response {

        @SerializedName("Interstial AD")
        public String interstialAd;

        public String getInterstialAd() {
            return interstialAd;
        }

        public void setInterstialAd(String interstialAd) {
            this.interstialAd = interstialAd;
        }
    }
}

And after this you can do something like:

String jsonToProcess = "your json string here";
ResponseModel model =  new Gson().fromJson(jsonResponse, ResponseModel.class);

String interstialAd = model.getResponse().getInterstialAd();

Note that there are many libraries for json manipulation beside GSon, like Jackson, Json from org.json, ...

Hope this helps you.

Iske
  • 1,150
  • 9
  • 18
  • thanks for your help, I tried your option 1 and it throws this error: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.arewang.app/com.domain.app.MainActivity}: com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 7 path $ – Steven Rõgrê Jul 12 '19 at 17:36
  • Check my edit for the code I wrote using your help. But the app crashes with the error I mentioned above. – Steven Rõgrê Jul 12 '19 at 17:38
  • Also paste your code in http://json.parser.online.fr/ to check to make sure the json is valid – RobOhRob Jul 12 '19 at 18:00
  • @RobOhRob I checked and it's valid. – Steven Rõgrê Jul 12 '19 at 19:17
  • 1
    @StevenRõgrê You have edited json from your question. Now it contains an array... I will edit my answer accordingly. – Iske Jul 13 '19 at 07:33
  • 1
    @StevenRõgrê I have edited Option 1 which you have used. Now it works with your json. – Iske Jul 13 '19 at 07:53
  • @Iske please refer to my question here: https://stackoverflow.com/questions/57013054/malformedjsonexception-use-jsonreader-setlenienttrue-to-accept-malformed-json – Steven Rõgrê Jul 13 '19 at 10:56
  • I'm trying to parse an online JSon – Steven Rõgrê Jul 13 '19 at 10:57