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

3

可能是重复问题:
如何防止HTTP转义引号?

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

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("\\\"", "\"")并不会影响字符串。


答案在http://stackoverflow.com/questions/6715650/how-can-i-stop-http-from-escaping-quotes。这不是Java的问题,而是PHP的问题。 - efpies
我现在使用Retrofit库也遇到了同样的问题,你找到解决方法了吗?还是你必须在服务器端处理它? - Ondrej Tokar
2个回答

1

看起来你正在使用UrlEncodedFormEntity,根据文档,它是“由一组url编码的键值对组成的实体”([http://developer.android.com/reference/org/apache/http/client/entity/UrlEncodedFormEntity.html])。我以前从未使用过这个,但它似乎不是你想要的,因为你是通过POST请求体发送数据,而不是通过URL参数。

我以前使用过StringEntity类通过post发送json数据,虽然它只编码一个字符串,而不是名称/值对,所以你需要做更多的工作将字符串放在你想要在服务器上处理的格式中:

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 {
        //TODO: Build a string in what ever format you want. 
        //      This will include the gender & feature_id fields as well as the json
        StringBuilder b = new StringBuilder();
        for(NameValuePair nvp : params) {
           builder.append(nvp.getName()).append('=').append(nvp.getValue()).append(',');
        }

        //Now that we have a string to send to the server, get your entity!
        StringEntity entity = new StringEntity(b.toString());
        entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        return entity;
    }
}

谢谢。但不幸的是,我必须在POST请求中发送其他参数以及JSON,并且它们都应该使用“application/x-www-form-urlencoded”内容类型。因此,我必须在服务器上替换转义引号... - efpies

0

使用单引号而不是双引号会有问题吗?因为我认为这样可以解决你的问题。


我必须使用双引号。JSON字段名称要用引号括起来。 - efpies
1
是的,但(至少在JavaScript中解析时),单引号与双引号一样有效。试试看。 - YuriAlbuquerque

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接