A
A
antazy2015-06-04 15:55:13
Java
antazy, 2015-06-04 15:55:13

Why is this java shit code?

Initial JSON
{"data":[{"permission":"publish_actions","status":"granted"}]}

String status = "";
try {
    JSONObject result = response.getJSONObject();
    status = ((JSONObject) ((JSONArray) result.get("data")).get(0)).getString("status");
} catch (JSONException e) {
    e.printStackTrace();
}
System.out.println(status);

What is wrong with this code?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Viktor Koltcov, 2015-06-04
@antazy

status = ((JSONObject) ((JSONArray) result.get("data")).get(0)).getString("status");

here all the evil is a bunch of brackets, explicit type casting .... not readable ...
PS
JSON-B is clearer (albeit slower)

A
alk, 2015-06-04
@Alexey_Kutepov

At least NullPointerException is likely to be caught if, for example, the array in JSON is empty. The same is true for the parameters - there is no check for their presence. In general, I advise you to use Gson for such purposes. Life will be much easier

S
scapp, 2015-06-04
@scapp

Because your boss is in a shitty mood!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question