Paula Kristin
Paula Kristin

Reputation: 863

Retrofit - removing escaped characters from json acquired from C#

I have this json string(?) that I got from calling a C# Api

"{\"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

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 :

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

Upvotes: 2

Views: 4718

Answers (1)

iagreen
iagreen

Reputation: 32026

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.

Upvotes: 2

Related Questions