JSON decoding in Java

I got the JSON (encoded) format of nested arrays that looks like this:

[ [[1234,245,10],[312,234,122],[1234,67788,345],[235,001,332]], [[1234,245,10],[312,234,122],[1234,67788,345],[235,001,332],[1234,67788,3450]], [[1234,245,10],[312,234,122],[1234,67788,345],[235,001,332],[1234,67788,34534]]] 

So, I have one large array that contains three arrays (sometimes it can be 2 or more than three arrays), and each of these three arrays contains some arrays in this example.

What is the reverse procedure (decoded format)? I mean, if I want these values ​​from these arrays.

I tried the org.json Java API, this is correct:

 JSON JSONArray list = new JSONArray(); list.get() 
+4
source share
4 answers
  someArray[] array = [ [[1234,245,10],[312,234,122],[1234,67788,345],[235,001,332]], [[1234,245,10],[312,234,122],[1234,67788,345],[235,001,332],[1234,67788,3450]], [[1234,245,10],[312,234,122],[1234,67788,345],[235,001,332],[1234,67788,34534]]] JSONArray superMasterArray = new JSONArray(array); if(superMasterArray != null){ for(int i = 0; i < superMasterArray.length(); i++ ){ JSONArray masterArray = (JSONArray) superMasterArray.get(i); for(int j = 0; j< masterArray.length(); j++){ JSONArray innerArray = (JSONArray) masterArray.get(j); innerArray.getint(0);// gives 1st element of the inner array that is 1234 innerArray.getint(1);// gives 245 innerArray.getint(2);// gives 10 // if you dont know how many element in the given array, then loop it with size of array } } } 
+1
source

There is json-lib for parsing JSON strings. See this article about O'Reilly . It also shows a maven based example, so a good source for Ctrl-C / Ctrl-V (or Cmd if you prefer).

+2
source

This might be a little redundant if you just need to parse the JSON array, but you can use JavaScript inside Java if you use Java 6. Then you can convert the JavaScript array to ArrayList or whatever you want to use on the Java side, as you can access to Java classes inside the built-in JavaScript function.

Embedding JavaScript in Java will look something like this:

 // create a script engine manager ScriptEngineManager factory = new ScriptEngineManager(); // create JavaScript engine ScriptEngine engine = factory.getEngineByName("JavaScript"); // evaluate JavaScript code engine.eval("function someFunction(param) {...}"); // you can also use an external script //engine.eval(new java.io.FileReader("/path/to/script.js")); // javax.script.Invocable is an optional interface. // Check whether your script engine implements or not! // Note that the JavaScript engine implements Invocable interface. Invocable inv = (Invocable) engine; // invoke your global function Object result = inv.invokeFunction("someFunction", param); 

If you want to do more than just convert a JSON array to a Java array, this might be useful. You can find more information in the Scripting section for the Java platform and the Java Scripting Programmer's Guide .

+2
source

There are several free classes at json.org for decoding and encoding JSON: http://www.json.org/java/index.html

+2
source

All Articles