package com.ironsource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/* loaded from: classes4.dex */
public class aq {

    /* renamed from: a  reason: collision with root package name */
    private JSONObject f2201a;

    public aq() {
        this.f2201a = new JSONObject();
    }

    public aq(String str) {
        f(str);
    }

    private Object a(Object obj) throws JSONException {
        if (obj == JSONObject.NULL) {
            return null;
        }
        return obj instanceof JSONObject ? b((JSONObject) obj) : obj instanceof JSONArray ? a((JSONArray) obj) : obj;
    }

    public static boolean a(JSONObject jSONObject) {
        return jSONObject.names() == null;
    }

    public static Object b(Object obj) throws JSONException {
        if (!(obj instanceof Map)) {
            if (obj instanceof Iterable) {
                JSONArray jSONArray = new JSONArray();
                for (Object obj2 : (Iterable) obj) {
                    jSONArray.put(obj2);
                }
                return jSONArray;
            }
            return obj;
        }
        JSONObject jSONObject = new JSONObject();
        Map map = (Map) obj;
        for (Object obj3 : map.keySet()) {
            if (obj3 != null) {
                jSONObject.put(obj3.toString(), b(map.get(obj3)));
            }
        }
        return jSONObject;
    }

    private Map<String, Object> b(JSONObject jSONObject) throws JSONException {
        HashMap hashMap = new HashMap();
        Iterator<String> keys = jSONObject.keys();
        while (keys.hasNext()) {
            String next = keys.next();
            hashMap.put(next, a(jSONObject.get(next)));
        }
        return hashMap;
    }

    private void f(String str) {
        try {
            this.f2201a = new JSONObject(str);
        } catch (Exception e) {
            l9.d().a(e);
            this.f2201a = new JSONObject();
        }
    }

    public int a(String str, int i) {
        return this.f2201a.optInt(str, i);
    }

    public String a(String str, String str2) {
        return this.f2201a.optString(str, str2);
    }

    public List a(JSONArray jSONArray) throws JSONException {
        ArrayList arrayList = new ArrayList();
        for (int i = 0; i < jSONArray.length(); i++) {
            arrayList.add(a(jSONArray.get(i)));
        }
        return arrayList;
    }

    public JSONObject a() {
        return this.f2201a;
    }

    public void a(String str, JSONObject jSONObject) {
        try {
            this.f2201a.put(str, jSONObject);
        } catch (Exception e) {
            l9.d().a(e);
        }
    }

    public boolean a(String str) {
        return a().has(str);
    }

    public Object b(String str) {
        try {
            return a().get(str);
        } catch (JSONException e) {
            l9.d().a(e);
            return null;
        }
    }

    public void b(String str, String str2) {
        try {
            this.f2201a.put(str, str2);
        } catch (Exception e) {
            l9.d().a(e);
        }
    }

    public boolean c(String str) {
        return this.f2201a.optBoolean(str);
    }

    public String d(String str) {
        return this.f2201a.optString(str, null);
    }

    public boolean e(String str) {
        return a().isNull(str);
    }

    public String toString() {
        JSONObject jSONObject = this.f2201a;
        return jSONObject == null ? "" : jSONObject.toString();
    }
}
