如何在json API中传递“&”符号

问题描述 投票:0回答:2

如何以json格式传递“&”符号。

输入在这里,

JSON:

{
    "sid":"sid2",
    "password":"a&b",
    "sec_type":"2"
}

在此期间我正在形成json,密码字段'a&b'在发送之前会自动转换为'\ u0026'。这让我陷入了困境。

我使用tojson()方法转换了模型。

android json
2个回答
0
投票

如果你正在使用Gson,那么

Gson gson = new GsonBuilder().disableHtmlEscaping().create();
gson.toJson(yourObject);

应该做的伎俩。


0
投票

我通过以下格式解决了这个问题谢谢大家.. :)

JSONObject jsonObject= new JSONObject();
try {
    jsonObject.put("ssid", getID());
    jsonObject.put("password", a&b());
    jsonObject.put("sec_type", getSecType());
    return jsonObject.toString();
} catch (JSONException e) {
    e.printStackTrace();
    return "";
}

现在它显然正是我发送的内容

© www.soinside.com 2019 - 2024. All rights reserved.