我在Android应用中使用Gson将复杂对象转换为JSON表示。其中一个字段是名为QuickPin的字符串,其中包含加密密码,字符“=”由Gson转换为“\ 003d”。
Json字符串由C#WEBAPI应用程序使用,但返回“发生错误”消息。
以下JSON返回该错误消息:
{"UserContractID":"929c1399-11c4-490e-8cff-5b1458ac18e2","UserAuthentication":"MethodCombo":{"AuthMethod":[1]},"QuickPin":"mW2n2uTECEtVqWA2B9MzvQ\u003d\u003d"},"CustomerID":0,"OriginID":0,"OriginTypeID":0,"Status":0}
同时这个JSON工作正常:
{"UserContractID":"929c1399-11c4-490e-8cff-5b1458ac18e2","UserAuthentication":{"QuickPin":"mW2n2uTECEtVqWA2B9MzvQ==","MethodCombo":{"AuthMethod":[1]}},"CustomerID":0,"OriginID":0,"OriginTypeID":0,"Status":0}
有没有办法强制Gson使用=(以及其他情况下是其他字符)字符的密码来维护字符串?
我的Android代码是:
Gson gson = new Gson();
user = new User();
user.UserAuthentication = new UserAuthentication();
user.UserAuthentication.QuickPin = "mW2n2uTECEtVqWA2B9MzvQ==";
user.UserAuthentication.MethodCombo = new MethodCombo();
user.UserAuthentication.MethodCombo.AuthMethod = new ArrayList<Integer>();
user.UserAuthentication.MethodCombo.AuthMethod.add(1);
user.Status = 0;
String jsonRepresentation = gson.toJson(user);
object.put("user", jsonRepresentation);
由于
答案 0 :(得分:7)
Gson默认转义HTML元字符。 You can disable this behavior
Gson gson = new GsonBuilder().disableHtmlEscaping().create();