简体   繁体   English

改造-从C#获取的json中删除转义字符

[英]Retrofit - removing escaped characters from json acquired from C#

I have this json string(?) that I got from calling a C# Api 我有通过调用C#Api得到的json字符串(?)

"{\"PublicApiToken\":\"M6RVJcCyiVODapF0wOR/Pg==\",\"ErrorList\":[]}"

This is returning an error of : 这将返回错误:

retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2 path $

Is there any this can be converted/cleaned? 有什么可以转换/清洗的吗? I've tried researching and the closest I got was this link : Retrofit - removing some invalid characters from response body before parsing it as json 我尝试研究,得到的最接近的链接是: 改装-在将响应主体解析为json之前,从响应主体中删除一些无效字符

but unfortunately it still doesn't work on my problem at all. 但不幸的是,它仍然根本无法解决我的问题。

Have you guys solved this problem? 你们解决了这个问题吗?

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 : 我的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();
    }
}

It appears only the quotes mark " is escaped. The best solution as mentioned in the comments is to get the server to fix it if possible. If you are stuck with it, This answer from the question you linked to gives you almost exactly what you need. You only need to adjust the meaning of the invalid character. 它只出现引号标记"被转义。如在评论中提到最好的解决办法是让可能的话服务器来修复它。如果你坚持了下来, 这个答案从问题您链接到让你几乎正是你您只需要调整无效字符的含义。

This line removes the leading and trailing () 's in the answer. 该行将删除答案中的前导和尾随()

String clean = dirty.replaceAll("(^\\(|\\)$)", "");

You want to replace \\" with " , so change the line above to -- 您想将\\"替换为" ,因此将上面的行更改为-

String clean = dirty.replaceAll("^\"|\"$","").replace("\\\"", "\"");

Note, this will only work if the assumption above about quotes being the only escape character is true. 请注意,这仅在以上关于引号是唯一转义字符的假设为真时才有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM