改造 - 从从 C# 获取的 json 中删除转义字符
Retrofit - removing escaped characters from json acquired from C#
我有这个 json 字符串 (?) 是我通过调用 C# Api
得到的
"{\"PublicApiToken\":\"M6RVJcCyiVODapF0wOR/Pg==\",\"ErrorList\":[]}"
返回错误:
retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $
有没有这个可以converted/cleaned?我试过研究,最接近的是这个 link : Retrofit - removing some invalid characters from response body before parsing it as json
但不幸的是,它仍然根本无法解决我的问题。
你们解决这个问题了吗?
我的方法调用:
@Headers("Content-Type: application/json")
@POST("/authorize/AcquirePublicApiToken")
void attemptLoginToMCCServer(@Header("Content-Type") String contentType, @Header("Authorization") String authorization, @Body Authorization authorizationKey, Callback<SuccessLoginCallback> successLoginCallback);
我的 Pojo :
public class Authorization {
private String consumerName;
private String username;
private String consumerKey;
private String password;
private String nonce;
private String timeStamp;
public String getConsumerName() {
return consumerName;
}
public void setConsumerName(String consumerName) {
this.consumerName = consumerName;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getConsumerKey() {
return consumerKey;
}
public void setConsumerKey(String consumerKey) {
this.consumerKey = consumerKey;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNonce() {
return nonce;
}
public void setNonce(String nonce) {
this.nonce = nonce;
}
public String getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(String timeStamp) {
this.timeStamp = timeStamp;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("amx ");
sb.append(getConsumerName());
sb.append("-");
sb.append(getUsername());
sb.append(":");
sb.append(getConsumerKey());
sb.append("-");
sb.append(getPassword());
sb.append(":");
sb.append(getNonce());
sb.append(":");
sb.append(getTimeStamp());
return sb.toString();
}
}
似乎只有引号 "
被转义了。评论中提到的最佳解决方案是让服务器尽可能修复它。如果您坚持使用它,您链接到的问题中的 This answer 几乎可以满足您的需求。您只需要调整无效字符的含义即可。
此行删除了答案中开头和结尾的 ()
。
String clean = dirty.replaceAll("(^\(|\)$)", "");
您想将 \"
替换为 "
,因此将上面的行更改为 --
String clean = dirty.replaceAll("^\"|\"$","").replace("\\"", "\"");
请注意,这仅在上述关于引号是唯一转义字符的假设成立时才有效。
我有这个 json 字符串 (?) 是我通过调用 C# Api
得到的"{\"PublicApiToken\":\"M6RVJcCyiVODapF0wOR/Pg==\",\"ErrorList\":[]}"
返回错误:
retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $
有没有这个可以converted/cleaned?我试过研究,最接近的是这个 link : Retrofit - removing some invalid characters from response body before parsing it as json
但不幸的是,它仍然根本无法解决我的问题。
你们解决这个问题了吗?
我的方法调用:
@Headers("Content-Type: application/json")
@POST("/authorize/AcquirePublicApiToken")
void attemptLoginToMCCServer(@Header("Content-Type") String contentType, @Header("Authorization") String authorization, @Body Authorization authorizationKey, Callback<SuccessLoginCallback> successLoginCallback);
我的 Pojo :
public class Authorization {
private String consumerName;
private String username;
private String consumerKey;
private String password;
private String nonce;
private String timeStamp;
public String getConsumerName() {
return consumerName;
}
public void setConsumerName(String consumerName) {
this.consumerName = consumerName;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getConsumerKey() {
return consumerKey;
}
public void setConsumerKey(String consumerKey) {
this.consumerKey = consumerKey;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNonce() {
return nonce;
}
public void setNonce(String nonce) {
this.nonce = nonce;
}
public String getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(String timeStamp) {
this.timeStamp = timeStamp;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("amx ");
sb.append(getConsumerName());
sb.append("-");
sb.append(getUsername());
sb.append(":");
sb.append(getConsumerKey());
sb.append("-");
sb.append(getPassword());
sb.append(":");
sb.append(getNonce());
sb.append(":");
sb.append(getTimeStamp());
return sb.toString();
}
}
似乎只有引号 "
被转义了。评论中提到的最佳解决方案是让服务器尽可能修复它。如果您坚持使用它,您链接到的问题中的 This answer 几乎可以满足您的需求。您只需要调整无效字符的含义即可。
此行删除了答案中开头和结尾的 ()
。
String clean = dirty.replaceAll("(^\(|\)$)", "");
您想将 \"
替换为 "
,因此将上面的行更改为 --
String clean = dirty.replaceAll("^\"|\"$","").replace("\\"", "\"");
请注意,这仅在上述关于引号是唯一转义字符的假设成立时才有效。