views:

115

answers:

4

I got a JSON(encoded) format nested Arrays which 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 big array which contains three arrays (this can be 2 or more than three arrays sometimes) and each of these three arrays contains some arrays, in this above example.

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

I tried org.json Java API, is this right:

JSON JSONArray list = new JSONArray();
list.get()
+2  A: 

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

Peter Tillemans
Thank you so much for the tutorial.
salman
you're welcome.
Peter Tillemans
+1  A: 
    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 
                }
            }
    }
salman
+2  A: 

It might be a little bit of an overkill if you just need to parse the JSON array, but you can use JavaScript within Java if you use Java 6. You could then convert the JavaScript array into an ArrayList or whatever you want to use on the Java side, since you can access Java classes inside the embedded JavaScript function.

Embedding JavaScript into Java would look similar to 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 your JSON array to a Java array, this might be useful. You can find more information under Scripting for the Java Platform and Java Scripting Programmer's Guide.

wtfc63
+1  A: 

On json.org are some free classes for decoding and encoding JSON: http://www.json.org/java/index.html

Mnementh