使用JSOUP发布嵌套JSON时出错

时间:2016-07-22 08:26:13

标签: java arrays json saiku

我想将嵌套的JSON发布到服务器中。但是我在postman中使用以下标题和嵌套的JSON获得了成功,并且在通过java代码发布时出错,我在下面添加了我的代码。

header("Content-Type", "application/json")
header("Accept", "application/json")

JSON

{
    "mdx": "",
    "metadata": {},
    "plugins": {},
    "name": "earth",
    "cube": {
        "schema": "Global Earthquakes",
        "uniqueName": "[earthquakes].[Global Earthquakes].[Global Earthquakes].[Earthquakes]",
        "visible": false,
        "catalog": "Global Earthquakes",
        "name": "Earthquakes",
        "connection": "earthquakes"
    },
    "queryModel": {
        "calculatedMembers": [],
        "calculatedMeasures": [],
        "lowestLevelsOnly": false,
        "axes": {
            "FILTER": {
                "aggregators": [],
                "hierarchies": [],
                "nonEmpty": true,
                "location": "FILTER",
                "filters": []
            },
            "COLUMNS": {
                "aggregators": [],
                "hierarchies": [],
                "nonEmpty": true,
                "location": "COLUMNS",
                "filters": []
            },
            "ROWS": {
                "aggregators": [],
                "hierarchies": [
                    {
                        "cmembers": {},
                        "name": "[Date].[Time]",
                        "levels": {
                            "Year": {
                                "name": "Year"
                            }
                        }
                    }
                ],
                "nonEmpty": true,
                "location": "ROWS",
                "filters": []
            }
        },
        "details": {
            "measures": [
                {
                    "name": "Average Magnitude",
                    "type": "EXACT"
                }
            ],
            "location": "BOTTOM",
            "axis": "COLUMNS"
        },
        "visualTotals": false
    },
    "type": "QUERYMODEL",
    "parameters": {},
    "properties": {
        "saiku.olap.query.nonempty": true,
        "saiku.olap.query.nonempty.rows": true,
        "org.saiku.query.explain": true,
        "saiku.olap.query.drillthrough": true,
        "saiku.olap.query.nonempty.columns": true,
        "saiku.olap.result.formatter": "flattened",
        "saiku.ui.render.mode": "table",
        "saiku.olap.query.automatic_execution": true,
        "saiku.olap.query.filter": true,
        "org.saiku.connection.scenario": false
    },
    "queryType": "OLAP"
}

这是我的代码

    // nested JSON
    JSONObject object = new JSONObject();
    JSONObject querymodel = new JSONObject();
    JSONObject cube = new JSONObject();
    JSONObject parameters = new JSONObject();
    JSONObject plugin = new JSONObject();
    JSONObject properties = new JSONObject();
    JSONObject metadata = new JSONObject();
    JSONObject axes = new JSONObject();
    JSONObject details = new JSONObject();
    JSONObject columns = new JSONObject();
    JSONObject filter = new JSONObject();
    JSONObject rows = new JSONObject();
    JSONObject measures = new JSONObject();
    JSONObject o = new JSONObject();
    JSONObject hierarchies = new JSONObject();
    JSONObject o2 = new JSONObject();
    JSONObject levels = new JSONObject();
    JSONObject Year = new JSONObject();
    JSONObject cmembers = new JSONObject();

    cube.put("caption", null);
    cube.put("catalog", "Global Earthquakes");
    cube.put("connection", "earthquakes");
    cube.put("name", "Earthquakes");
    cube.put("schema", "Global Earthquakes");
    cube.put("uniqueName",
            "[earthquakes].[Global Earthquakes].[Global Earthquakes].[Earthquakes]");
    cube.put("visible", Boolean.FALSE);

    properties.put("org.saiku.connection.scenario", Boolean.FALSE);
    properties.put("org.saiku.query.explain", Boolean.TRUE);
    properties.put("saiku.olap.query.automatic_execution", Boolean.TRUE);
    properties.put("saiku.olap.query.drillthrough", Boolean.TRUE);
    properties.put("saiku.olap.query.filter", Boolean.TRUE);
    properties.put("saiku.olap.query.nonempty", Boolean.TRUE);
    properties.put("saiku.olap.query.nonempty.columns", Boolean.TRUE);
    properties.put("saiku.olap.query.nonempty.rows", Boolean.TRUE);
    properties.put("saiku.olap.result.formatter", "flattened");
    properties.put("saiku.ui.render.mode", "table");


    List<String> a = new ArrayList<String>();
    List<String> ab = new ArrayList<String>();

    querymodel.put("axes", axes);
    querymodel.put("calculatedMeasures", a);
    querymodel.put("calculatedMembers", ab);
    querymodel.put("details", details);
    querymodel.put("lowestLevelsOnly", Boolean.FALSE);
    querymodel.put("visualTotals", Boolean.FALSE);
    querymodel.put("visualTotalsPattern", null);

    axes.put("COLUMNS", columns);
    axes.put("FILTER", filter);
    axes.put("ROWS", rows);

    List<Object> oa = new ArrayList<Object>();
    oa.add(o);
    o.put("name", "Average Magnitude");
    o.put("type", "EXACT");

    details.put("axis", "COLUMNS");
    details.put("location", "BOTTOM");
    details.put("measures", oa);

    List<String> b = new ArrayList<String>();
    List<String> bb = new ArrayList<String>();
    List<String> bc = new ArrayList<String>();

    columns.put("aggregators", b);
    columns.put("filters", bb);
    columns.put("hierarchies", bc);
    columns.put("hierarchizeMode", null);
    columns.put("location", "COLUMNS");
    columns.put("mdx", null);
    columns.put("nonEmpty", Boolean.TRUE);
    columns.put("sortEvaluationLiteral", null);
    columns.put("sortOrder", null);

    List<String> c = new ArrayList<String>();
    List<String> cb = new ArrayList<String>();
    List<String> cc = new ArrayList<String>();

    filter.put("aggregators", c);
    filter.put("filters", cb);
    filter.put("hierarchies", cc);
    filter.put("hierarchizeMode", null);
    filter.put("location", "FILTER");
    filter.put("mdx", null);
    filter.put("nonEmpty", Boolean.TRUE);
    filter.put("sortEvaluationLiteral", null);
    filter.put("sortOrder", null);

    List<String> d = new ArrayList<String>();
    List<String> db = new ArrayList<String>();
    List<Object> o2a = new ArrayList<Object>();
    o2a.add(o2);
    o2.put("cmembers", cmembers);
    o2.put("levels", levels);
    o2.put("name", "[Date].[Time]");

    rows.put("aggregators", d);
    rows.put("filters", db);
    rows.put("hierarchies", o2a);
    rows.put("hierarchizeMode", null);
    rows.put("location", "ROWS");
    rows.put("mdx", null);
    rows.put("nonEmpty", Boolean.TRUE);
    rows.put("sortEvaluationLiteral", null);
    rows.put("sortOrder", null);

    //hierarchies.put("0", o2a);

    levels.put("Year", Year);

    Year.put("name", "Year");


    String abcd ="SELECT\r\nFROM [Earthquakes]";

    object.put("cube", cube);
    object.put("mdx","");
    object.put("metadata", metadata);
    object.put("name", "earth");
    object.put("parameters", parameters);
    object.put("plugins", plugin);
    object.put("properties", properties);
    object.put("queryModel", querymodel);
    object.put("queryType", "OLAP");
    object.put("type", "QUERYMODEL");

    HashMap<String, Object> objectH = new HashMap<String, Object>();
    objectH.put("cube", cube);
    objectH.put("mdx","");
    objectH.put("metadata", metadata);
    objectH.put("name", "earth");
    objectH.put("parameters", parameters);
    objectH.put("plugins", plugin);
    objectH.put("properties", properties);
    objectH.put("queryModel", querymodel);
    objectH.put("queryType", "OLAP");
    objectH.put("type", "QUERYMODEL");

   /* List<KeyVal> list = new ArrayList<KeyVal>();
    list.add(KeyVal.create("cube", cube.toString()));*/

     Gson gson = new Gson();
     String json = gson.toJson(objectH);

     System.out.println("json string++++++++++"+json);

     String obj[];
     obj = new String[]{json};
     System.out.println("string array++++++"+obj);



    //System.out.println("++++++++++++++++++++++++++++"+object.toString());


    Response execute = Jsoup
            .connect(
                    "http://localhost:8000/saiku/rest/saiku/api/query/execute/")
            .header("Content-Type", "application/json")
            .header("Accept", "application/json")
            .requestBody(objectH.toString())
            .data(new String[]{json})               //here i got error
            .ignoreContentType(true)
            .referrer("http://localhost:8000/")
            .cookie("JSESSIONID", res.cookie("JSESSIONID"))
            .userAgent(
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36")
            .method(Method.POST).timeout(10000).execute();
    System.out.println("post successfully....."
            + object.toString());

如果我运行此代码意味着我收到错误

Errorjava.lang.IllegalArgumentException: Must supply an even number of key value pairs

1 个答案:

答案 0 :(得分:0)

使用data,您可以指定查询参数。字符串数必须是偶数,它们被解释为键值对。

我猜您的通话不需要查询参数,因此您可以将其完全删除。