2016-03-01 104 views
1

从下面的JSON中,我想要显示查询字符串。我通过使用JSON解析器完成了它。JSON对象在发送JSON请求时给出错误

{ 
    "group By": "name", 
    "time Period": { 
     "from": "2015-12-29", 
     "to": "2016-02-29" 
    }, 
    "query String": "[nation]: \"India\" AND [education]: \"be", 
    "geography": "NA", 
    "offset": 0, 
    "limit": 10 
} 

在运行时,它会显示错误的

Exception in thread "main" org.json.JSONException: JSONObject["query String"] is not a JSONObject. 

我的JSON查询:

String request = rs.getString("Request"); 
JSONObject jsonObject = new JSONObject(request); 
JSONObject newJSON = jsonObject.getJSONObject("groupBy"); 
String input = newJSON.toString(); 
+1

你的代码'查询String'不问的话 - 虽然它不问'GROUPBY '好像它是一个JSON对象,它不是,并且名称错误。您提供的代码与您显示的错误不符。请提供[mcve]。 –

回答

1

如果你只是想获得你的JSON的 “查询字符串”。我得到了它的代码,这部分工作:

String json = "{ \"group By\": \"name\", \"time Period\": { \"from\": \"2015-12-29\", \"to\": \"2016-02-29\" }, \"query String\": \"[nation]: \\\"India\\\" AND [education]: \\\"be\\\"\", \"geography\": \"NA\", \"offset\": 0, \"limit\": 10 }"; 
JSONObject jsonObject = new JSONObject(json); 
String input = (String) jsonObject.get("query String"); 

input输出是:

[nation]: "India" AND [education]: "be"