0

I get the following JSON response from a server...

[["1","1"],["2","1"],["3","1"],["4","1"],["5","1"],["6","1"],["7","1"],["8","1"],["9","2"],["10","3"],["11","3"],["12","3"],["13","3"],["14","3"],["15","3"],["16","3"],["17","3"],["18","3"],["19","3"]]

It is in JSON format and i get it as a String[], something like this...

String response = Response.getValue();
System.out.println(response) = [["1","1"],["2","1"],["3","1"],["4","1"],["5","1"],["6","1"],["7","1"],["8","1"],["9","2"],["10","3"],["11","3"],["12","3"],["13","3"],["14","3"],["15","3"],["16","3"],["17","3"],["18","3"],["19","3"]]

However, the response is a matrix of 2 values [USU_ID, DEPARTMENT] and i need to use it in a String[][]. How can I do this? I tried to use StringTokenizer but it doesn't work very well.

This is the code I have written...

public static String[][] Json2Matrix(String jsonStringArray) {
        int i = 0;
        int j = 0;

        String[][] mstrJsonString = null;
        StringTokenizer tokElementos, tokSubelementos, tokTemp;
        //jsonArray = "[["a","b"],["c","d"],["e","f"]]";
        //jsonStringArray = jsonStringArray.replace("\"", "");
        //jsonArray = "[[a,b],[c,d],[e,f]]";
        jsonStringArray = jsonStringArray.substring(1, jsonStringArray.length() - 2);
        //jsonArray = "[a,b],[c,d],[e,f]";/

        //System.out.println(jsonStringArray);

        //JSONSerializer.toJSON(jsonStringArray);

        //System.out.println(jsonArray.toString());

        //<editor-fold defaultstate="collapsed" desc="Prueba">
        tokElementos = new StringTokenizer(jsonStringArray, "[]");

        tokTemp = tokElementos;
        tokSubelementos = new StringTokenizer(tokTemp.nextToken(), ",");

        //System.out.println(tokElementos.countTokens());

        //System.out.println(tokElementos.nextToken());

        //System.out.println(tokSubelementos.countTokens()/2);

        mstrJsonString = new String[tokElementos.countTokens()][tokSubelementos.countTokens()];

        while (tokElementos.hasMoreTokens()) {
            tokSubelementos = new StringTokenizer(tokElementos.nextToken(), ",");
            j = 0;
            while (tokSubelementos.hasMoreTokens()) {

                mstrJsonString[i][j] = tokSubelementos.nextToken();
                System.out.println(i + "," + j + " " + mstrJsonString[i][j]);
                j++;
            }
            i++;

        }
        //</editor-fold>
        return mstrJsonString;
    }

and i get this as the output...

run:
1,0 "2"
1,1 "1"
3,0 "3"
3,1 "1"
5,0 "4"
5,1 "1"
7,0 "5"
7,1 "1"
9,0 "6"
9,1 "1"
11,0 "7"
11,1 "1"
13,0 "8"
13,1 "1"
15,0 "9"
15,1 "2"
17,0 "10"
17,1 "3"
19,0 "11"
19,1 "3"
21,0 "12"
21,1 "3"
23,0 "13"
23,1 "3"
25,0 "14"
25,1 "3"
27,0 "15"
27,1 "3"
29,0 "16"
29,1 "3"
31,0 "17"
31,1 "3"
33,0 "18"
33,1 "3"
35,0 "19"
35,1 "3"

It gets the correct values, but the incorrect Indexes. Could someone please help me correct this.

3
  • 2
    I'm sorry, do you want to write your own json parser? Why not use code.google.com/p/google-gson Commented Jun 15, 2012 at 15:49
  • String[][] nuevo = JsonParser.fromJson(jsonStringArray, String[][].class); that was my solution.... oh god, sry for my stupid question... im very junior in this. Thanks lukas :) Commented Jun 15, 2012 at 16:08
  • in C# I would say that you're deserializing a List<List<string>> if that helps at all... Commented Jun 16, 2012 at 7:46

2 Answers 2

1

Using a JSON parsers saves you a lot of effort than writing a primitive one yourself.

I have used these Java based JSON parsers:

  1. Jackson [http://jackson.codehaus.org/]
  2. Google's GSON [http://code.google.com/p/google-gson/]

I recommend Google's GSON as :

  1. You can serialize/deserialize json strings into Java Objects [POJOs], without annotations [uses variable names].
  2. Can easily serialize/deserialize deep object hierarchies.
  3. It has decent support for Java Generics
Sign up to request clarification or add additional context in comments.

Comments

0

http://json.org/java/

Hope this helps, I was using this a few months ago for using JSON with java and it worked really well(*also was using netbeans)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.