改进-从json中删除从c#获得的转义字符
本文关键字:转义字符 删除 json 改进 | 更新日期: 2023-09-27 18:17:09
我有这个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 $
有可以转换/清洗的吗?我试过研究,我得到的最接近的是这个链接:改造-从响应体中删除一些无效字符,然后解析为json
但不幸的是,它仍然不能解决我的问题。
你们解决这个问题了吗?
My Method Call:
@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);
My 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();
}
}
似乎只有引号"
被转义了。正如评论中提到的,最好的解决方案是让服务器尽可能修复它。如果你被它困住了,这个问题的答案几乎正好给了你所需要的。您只需要调整无效字符的含义。
这一行删除了答案中开头和结尾的()
。
String clean = dirty.replaceAll("(^''(|'')$)", "");
您想将'"
替换为"
,因此将上面的行更改为——
String clean = dirty.replaceAll("^'"|'"$","").replace("'''"", "'"");
注意,这只会在上面关于引号是唯一转义字符的假设为真时才会起作用。