从JSON响应中获取String [] []数组

时间:2012-06-15 15:48:47

标签: java json string

我从服务器获得以下JSON响应......

[["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"]]

它采用JSON格式,我将其作为String[]得到,就像这样......

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"]]

但是,响应是2个值[USU_ID, DEPARTMENT]的矩阵,我需要在String[][]中使用它。我怎样才能做到这一点?我尝试使用StringTokenizer,但效果不佳。

这是我写的代码......

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;
    }

我将此作为输出......

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"

它获取正确的值,但索引不正确。有人可以帮我纠正这个。

2 个答案:

答案 0 :(得分:1)

使用JSON解析器比自己编写原始解析器节省了很多精力。

我使用过这些基于Java的JSON解析器:

  1. 杰克逊[http://jackson.codehaus.org/]
  2. Google的GSON [http://code.google.com/p/google-gson/]
  3. 我推荐谷歌的GSON:

    1. 您可以将json字符串序列化/反序列化为Java对象[POJOs],而无需注释[使用变量名称]。
    2. 可以轻松地序列化/反序列化深层对象层次结构。
    3. 它对Java Generics提供了不错的支持

答案 1 :(得分:0)

http://json.org/java/

希望这会有所帮助,几个月前我使用它来使用JSON和java一起工作得很好(*也使用netbeans)