Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何添加json以获取请求查询参数?_Java_Json_Http_Url_Get - Fatal编程技术网

Java 如何添加json以获取请求查询参数?

Java 如何添加json以获取请求查询参数?,java,json,http,url,get,Java,Json,Http,Url,Get,我希望发送json以获取查询参数中的请求,从而获取该json请求的响应 如果我使用这样的链接: www.url.com/search?query1=abc&filter={“或”:[{“术语”:{“vin”:[“1g1105sa2gu104086”]}} 如果我按照sysout语句执行,url部分将显示为蓝色,如下所示: 筛选器={“或”:[{“术语”:{“vin”:[“1g1105sa2gu104086”]}} json看起来好像不是请求的一部分 为了创建一个URL,我将操纵的JSON字符串附

我希望发送json以获取查询参数中的请求,从而获取该json请求的响应

如果我使用这样的链接:

www.url.com/search?query1=abc&filter={“或”:[{“术语”:{“vin”:[“1g1105sa2gu104086”]}}

如果我按照sysout语句执行,url部分将显示为蓝色,如下所示:

筛选器={“或”:[{“术语”:{“vin”:[“1g1105sa2gu104086”]}}

json看起来好像不是请求的一部分

为了创建一个URL,我将操纵的JSON字符串附加到URL,然后发送请求。但它显示为两个不同的字符串

我还使用编码器对JSON部分进行编码

筛选器={“或”:[{“术语”:{“vin”:[“1g1105sa2gu104086”]}}

在这种情况下,括号和双引号将对json中的所有内容进行编码,甚至包括等号。同样,链接显示为蓝色,但在发送请求时抛出400错误请求的异常,因为equalTo也转换为其编码格式

我尝试只对JSON部分进行编码,将
过滤器=
保留在url本身中,如下所示:

{“或”:[{“术语”:{“vin”:[“1g1105sa2gu104086”]}}}

发送请求后显示的结果与我想要的结果不同

我使用以下代码创建JSON:

private String getVinFromInventoryRequest(String vin) throws JSONException {
    JSONObject request = new JSONObject();
    JSONArray orArray = new JSONArray();
    for(String vin : vins) {
        JSONObject termsObject = new JSONObject();
        JSONObject vinsObject = new JSONObject();
        JSONArray vinsArray = new JSONArray();
        vinsArray.put(vin);
        vinsObject.put("vin", vinsArray);
        termsObject.put("terms", vinsObject);
        orArray.put(termsObject);
    }
    request.put("or", orArray);
    System.out.println("OfferMapper.getVinFromInventoryRequest " + request.toString());
    return request.toString();
}

再看看我在谷歌上发现了什么:

JSONObject json = new JSONObject();
json.put("someKey", "someValue");    

CloseableHttpClient httpClient = HttpClientBuilder.create().build();

try {
    HttpPost request = new HttpPost("http://yoururl");
    StringEntity params = new StringEntity(json.toString());
    request.addHeader("content-type", "application/json");
    request.setEntity(params);
    httpClient.execute(request);
// handle response here...
} catch (Exception ex) {
    // handle exception here
} finally {
    httpClient.close();
}

有关更多信息,请参见:

另请查看我通过谷歌搜索发现的内容:

JSONObject json = new JSONObject();
json.put("someKey", "someValue");    

CloseableHttpClient httpClient = HttpClientBuilder.create().build();

try {
    HttpPost request = new HttpPost("http://yoururl");
    StringEntity params = new StringEntity(json.toString());
    request.addHeader("content-type", "application/json");
    request.setEntity(params);
    httpClient.execute(request);
// handle response here...
} catch (Exception ex) {
    // handle exception here
} finally {
    httpClient.close();
}

有关更多信息,请参见:

如何对json进行编码?编码的结果是什么?你能提供一个例子吗?重复的:我用java中的URLEncoder对json进行编码。它以这种格式编码:过滤器%3D%7B%22和%22%3A%5B%7B%22terms%22%3A%7B%22vin%22%3A%5B%221g1105sa2gu104086%22%5D%7D%7D%5D%7D,而不是创建每个带有一些humar错误的json字符串。我根据需要使用JSONObject和JSONArray。检查问题中的代码以创建json。我得到以下错误:意外字符('\'(代码92)):如果我尝试使用链接中给出的@PankajVerma解决方案,则应在[Source:{\'或\':[{\'术语\':{\'vin\':[\'1g1105sa2gu104086\']}处以双引号开头字段名。如何对json进行编码?编码的结果是什么?你能提供一个例子吗?重复的:我用java中的URLEncoder对json进行编码。它以这种格式编码:过滤器%3D%7B%22和%22%3A%5B%7B%22terms%22%3A%7B%22vin%22%3A%5B%221g1105sa2gu104086%22%5D%7D%7D%5D%7D,而不是创建每个带有一些humar错误的json字符串。我根据需要使用JSONObject和JSONArray。检查问题中的代码以创建json。我得到以下错误:意外字符('\'(代码92)):如果我尝试使用链接中给出的@PankajVerma解决方案,则应在[Source:{\'或\':[{\'术语\':{\'vin\':[\'1g1105sa2gu104086\']}处以双引号开头字段名;第1行,第3列]。