gpt4 book ai didi

Java 在 POST 请求中发送带有转义双引号的 JSON 字符串

转载 作者:太空狗 更新时间:2023-10-29 14:27:08 26 4
gpt4 key购买 nike

<分区>

Possible Duplicate:
How can I stop HTTP from escaping quotes?

我正在创建一个 JSONObject 并在 POST 请求正文中将 JSON 字符串发送到服务器。

public String toJson() {
JSONObject filter = new JSONObject();
try {
filter.put("gender", gender.getCode());
filter.put("feature_id", productCategory);
} catch (JSONException e) {
e.printStackTrace();
}

JSONObject filterObject = new JSONObject();
try {
filterObject.put("filter", filter);
} catch (JSONException e) {
e.printStackTrace();
}

return filterObject.toString();
}

所以我正在创建一个请求:

private IJsonExecutorInterface requestExecutorForRelativePathAndParams(String path, WebParams params) throws UnsupportedEncodingException {
HttpPost postRequest = new HttpPost(rootUrl + path);

if(params != null) {
postRequest.setHeader("content-type", params.getContentType());
postRequest.setEntity(params.getFormEntity());
}

// Blah blah

return executor;
}

public IJsonExecutorInterface getProducts(ProductFilter filter, int offset, int limit) throws UnsupportedEncodingException {
WebParams webParams = new WebParams();
webParams.addPair("filter", filter.toJson());
webParams.addPair("offset", String.format("%d", offset));
webParams.addPair("limit", String.format("%d", limit));
return requestExecutorForRelativePathAndParams("products", webParams);
}

// WebParams class


public class WebParams {
private ArrayList<NameValuePair> params;
private String contentType = "application/x-www-form-urlencoded";

public WebParams() {
params = new ArrayList<NameValuePair>();
}

public void addPair(String name, String value) {
params.add(new BasicNameValuePair(name, value));
}

public String getContentType() {
return contentType;
}

public HttpEntity getFormEntity() throws UnsupportedEncodingException {
return new UrlEncodedFormEntity(params);
}
}

我在调试器中看到它:没问题。

但是在我的服务器上我得到了这样的东西:

Array
(
[filter] => {\"gender\":\"w\",\"feature_id\":\"41_7459\"}
[offset] => 0
[limit] => 18
)

引号被转义了。

我不想替换服务器上的东西。 Java 中的 replace("\\\"", "\"") 不影响字符串。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com