0

I have been having a hard time getting some value from my encoded JSONObject in order to decode it.

server.json data file:

{
  "servers": [
    {
      "name": "Server 1",
      "domain": "157.230.228.67",
      "port": 465,
      "location": "ph",
      "http_server": "157.230.228.67",
      "http_port": 8000
    },
    {
      "name": "Server 2",
      "domain": "157.230.228.67",
      "port": 465,
      "location": "ph",
      "http_server": "10.199.212.2",
      "http_port": 8080
    }
  ],
  "payloads": [
    {
      "name": "Payload 1",
      "host": "IWdvb2dsZS5jb20="
    },
    {
      "name": "Payload 2",
      "host": "IWdvb2dsZS5jb20="
    },
    {
      "name": "Payload 3",
      "host": "IWdvb2dsZS5jb20="
    }
  ]
}

Now I only want to call and decode the (host)value maybe using jsonObject or JSONArray; A code snippet of my Mainactivity Class; This load the server.json on the main activity.

server_file = FileUtility.readFileFromAssets(getAssets(), "servers.json"); // LOAD
        Log.e("List", "onCreate: " + server_file);






    JSONObject jsonObject = null;
            try {
                jsonObject = new JSONObject(server_file);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            JSONArray key = jsonObject.names();
            for (int i = 0; i <key.length() ; i++) {
                try {
                    String keys = key.getString(i);
                    String value = jsonObject.getString(keys);
                    byte[] decode = Base64.getDecoder().decode(value);
                    String showDecode = new String(decode, "UTF-8");
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                try {
                    Log.d(TAG, "key: " + jsonObject.names().getString(i) + "value: " + jsonObject.get(jsonObject.names().getString(i)));
                } catch (JSONException e) {
                    e.printStackTrace();
                }

Now my main issue is how to call for the specific value which is (host) value in order to base64-decode it before it gets launched on my main activity.

+The value of the host before encoding is a String data type.

kenny_k
  • 3,831
  • 5
  • 30
  • 41
Jamsakino
  • 45
  • 9
  • off-topic comment: why `JSONArray key = jsonObject.names();` is outside `try` ? **for getting nice NPE if parsing fail?** – Selvin Sep 19 '19 at 13:44
  • 4
    Base64 is no encryption. It's an encoding. https://danielmiessler.com/study/encoding-encryption-hashing-obfuscation/ – dpr Sep 19 '19 at 13:48
  • 1
    As @Selvin said, you are not using `JSONObject`/`JSONArray` correctly to retrieve your values. You must do this before you can decode them - the decoding part looks fine, but you're not decoding the right things. This answer might be helpful: https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java – kenny_k Sep 19 '19 at 13:51

2 Answers2

0

You might benefit from letting gson convert this for you to objects/data classes (in kotlin). You can deserialize the base64 during it's creation via a custom JsonDeserializer/JsonSerializer.

Brandon McAnsh
  • 992
  • 8
  • 18
0

You can use the following to get the encoded 'host' value:

JSONArray mArray;
        try {
            mArray = new JSONArray(responseString);
             for (int i = 0; i < mArray.length(); i++) {
                    JSONObject mJsonObject = mArray.getJSONObject(i);
                    Log.d("Host", mJsonObject.getString("host"));
                }
        } catch (JSONException e) {
            e.printStackTrace();
        }

Then you can decode it.

Jagar
  • 777
  • 9
  • 24
  • First you have to get `payloads` array from the response string - the response is an object with 2 properties, not an array – kenny_k Sep 19 '19 at 14:35