有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用DropWizard对动态模式进行java JSON解析

我需要用动态模式分析JSON,并相应地做出响应

动态模式就像

{
"query": {
    <OPERATOR>: {
        <FIELD>: <VALUE>
    }
},
"fields" : [<FIELD_1>, <FIELD_2>, ...],
"from": 0,
"size": 10
}

或者

{
"query": {
    "and": [
        {
            <OPERATOR>: { 
                <FIELD>: <VALUE> 
            }
        },
        {
            <OPERATOR>: {
                    <FIELD>: <VALUE>
                }
        }
    ]
},
"fields" : [<FIELD_1>, <FIELD_2>, ...],
"from": 0,
"size": 10
}

我们的回应将基于上述JSON's。每个JSON类型的响应都会不同。 我可以使用dropwizard的Jackson库来解析还是需要编写自己的解析器是一个难题。如果是,怎么做

提前谢谢


共 (1) 个答案

  1. # 1 楼答案

    如果你在我的代码中卡住了,请告诉我,因为时间不够,我没有完善它

    String result = response.toString();
        Object mainResponse = null;
        try {
            mainResponse = new JSONTokener(result).nextValue();
            if (mainResponse instanceof JSONArray) {
                JSONArray json1 = new JSONArray(response.toString());
                Object value2 = json1.get(0);
                Object output = tryMethod((JSONObject) value2, searchString);
                outputtv.setText(output.toString());
                Log.i("Output", output.toString() + "");
            }
            if (mainResponse instanceof JSONObject) {
                JSONObject json = new JSONObject(response.toString());
                Object output1 = tryMethod(json, searchString);
                outputtv.setText(output1.toString());
                Log.i("Output", output1 + "");
            }
    
        } catch (JSONException e1) {
            e1.printStackTrace();
        }