0

I'm writing an app that uses JSON (from a Kimonolabs API) and while I was able to utilize my API effortlessly in Python as such:

results = json.loads(urllib.request.urlopen(KimonoAPIlink).read().decode('utf-8'))

Which returns a useable JSON dictionary:

title = results['results']['suart'][0]['stitle']['text']
href = results['results']['suart'][0]['stitle']['href']

In Java (Android specifically) I have to do it this way (Using Gson):

URL url = new URL(KimonoAPIlink);
HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.connect();
JsonParser jp = new JsonParser(); //from gson
JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent()));
JsonObject rootobj = root.getAsJsonObject(); //May be an array, may be an object.
JsonElement x = rootobj.getAsJsonObject("results").getAsJsonArray("suart").get(0);
JsonObject y = x.getAsJsonObject();
title.setText(y.getAsJsonObject("stitle").getAsJsonPrimitive("text").toString().replace("\"", "").replace("\\","\""));

Is there any way to do this that isn't so verbose and complicated? (my code works, I'd just like it to be more simple and clean)

Moreover, can I somehow parse the whole nested JSON object into something useful (like python does) and not reparse it each layer I access?

Thanks in advance.

Edit: I've also seen people use Apache commons.io on here for this, but I don't think it returns a different JSON object (i.e. I'd still have to parse every layer)

Yoav Lavi
  • 59
  • 6

2 Answers2

1

Check out GSON, Jackson or Moshi

Geralt_Encore
  • 3,721
  • 2
  • 31
  • 46
0

What you are looking for is lookup using JsonPath, wherein JSON coordinates are represented by a string with syntax pretty similar to the one you are using in Python (as well as other featuress).

For example, in your case it would be $.results.suart[o].stitle.text.

A quick look in the docs and the gson github shows it is not implemented in gson, although the JsonReader class does support a method to return that value.

There is a library implementation JsonPath in java (called JsonPath). Here's a one line code for your case (ignoring connection detail):

String result = JsonPath.parse("[some json]").read("$.results.suart[o].stitle.text");
SilverCorvus
  • 2,956
  • 1
  • 15
  • 26
  • Tried this, I get an Error:Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\JDK_64_bit\bin\java.exe'' finished with non-zero exit value 2 – Yoav Lavi Oct 25 '15 at 21:06
  • That's a gradle error, which means that for whatever reason there was an error adding the library to your project (maybe reached the method limit, see ), not really related to the question at hand. Still, you might be able to use this solution in android, see http://stackoverflow.com/questions/28917696/ – SilverCorvus Oct 28 '15 at 22:01
  • I've opened a separate question for this error a while ago, doesn't seem to be getting answers though. Tried MultiDex, doesn't help. http://stackoverflow.com/questions/33353508/adding-jsonpath-in-android-studio-causes-non-zero-exit-value-2-error – Yoav Lavi Oct 28 '15 at 22:03