【问题标题】:I'd like to know how to parse json to retrieve the admob ID我想知道如何解析 json 来检索 admob ID
【发布时间】:2019-07-11 20:22:13
【问题描述】:

我想知道如何解析一个 json 对象,该对象将上传到我的服务器以从中检索 admob 广告 ID。

例子:

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

将发送到

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();
    }
}

任何帮助将不胜感激!

编辑:

累了:

  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());
                }

            });
        }

我的 Json 文件:

{
    "response": [
        {
            "Interstial AD": "ca-app-pub-3940256099942544/1033173712"
        },
        {
            "Banner AD": "ca-app-pub-3940256099942544/6300978111"
        }
  ]
}

【问题讨论】:

  • 如果你只是想从josn获取数据,你可以使用一些jar来解析json,例如fastjson,gson,然后你就可以轻松获取数据了。

标签: java json


【解决方案1】:

使用Gson 很容易做到这一点...

选项 1

编辑每个问题编辑:

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;
    }
}

选项 2

创建响应模型并反序列化您的响应...

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;
        }
    }
}

在此之后,您可以执行以下操作:

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

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

请注意,除了 GSon 之外,还有许多用于 json 操作的库,例如 Jackson、来自 org.json 的 Json、...

希望对你有所帮助。

【讨论】:

  • 感谢您的帮助,我尝试了您的选项 1,但它抛出此错误:java.lang.RuntimeException: Unable to start activity ComponentInfo{com.arewang.app/com.domain.app.MainActivity} : com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: 使用 JsonReader.setLenient(true) 在第 1 行第 7 列路径 $ 接受格式错误的 JSON
  • 检查我使用您的帮助编写的代码的编辑。但是应用程序因我上面提到的错误而崩溃。
  • 还将您的代码粘贴到 json.parser.online.fr 以检查以确保 json 有效
  • @StevenRõgrê 你已经从你的问题中编辑了 json。现在它包含一个数组...我将相应地编辑我的答案。
  • @StevenRõgrê 我已经编辑了您使用的选项 1。现在它适用于您的 json。
猜你喜欢
  • 2018-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-29
  • 2013-05-09
  • 2019-08-13
  • 2020-04-23
相关资源
最近更新 更多