0

I am suffering from burnout messing with this backlashes...

I made a POST request in C++ and receive the following string:

"{\"results\": [{\"sku\": \"peds-std-us\", \"url\": \"https://d2y247jlvj8usn.cloudfront.net/1/2014.06.12/upload test.xlsx\", \"version\": \"2014.06.12\"}]}"

Then I process it like a rapidjson document but I get Parse Errors.

Document document;
if(document.Parse(results.c_str()).HasParseError()){
    return 1;
}

I tried removing the begin and end ""

like this

{\"results\": [{\"sku\": \"peds-std-us\", \"url\": \"https://d2y247jlvj8usn.cloudfront.net/1/2014.06.12/upload test.xlsx\", \"version\": \"2014.06.12\"}]}

Same problem...

Does anyone know what could be the problem?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Claudiordgz
  • 3,023
  • 1
  • 21
  • 48
  • 1
    What happens if you strip the `\ ` characters? Does it have a parse error then? Also test the begin and end `"` characters. – Mooing Duck Aug 19 '14 at 21:24
  • Thanks man, that is what I am currently trying... but using str.replace gives me a crash – Claudiordgz Aug 19 '14 at 21:32
  • 1
    `str.replace` should not cause a crash. – Mooing Duck Aug 19 '14 at 21:40
  • The problem is that this simply is not JSON. I guess someone added quotes (or is that in fact a C++ string literal?) in order to keep some other piece of software from interpreting the intermediate double quotes as delimiters of the string. In any case, the I believe you should split your problem in finding out how to retrieve proper JSON and finding out how to parse JSON. – Ulrich Eckhardt Aug 19 '14 at 21:45
  • it is a c++ string literal, if you remove the \ and the first and last " you get valid JSON. But its killing me. – Claudiordgz Aug 19 '14 at 21:52

1 Answers1

0

The reason why the JSON fails to being parsed by rapidjson it's because of all the excess backlashes \ and the first and last double quotes ". Thus I remove them as follows.

if (*resultsCopy.begin() == '"')
    if (*(resultsCopy.rbegin()) == '"')
        resultsCopy = resultsCopy.substr(1, resultsCopy.length() - 2);
    else
        resultsCopy = resultsCopy.substr(1, resultsCopy.length() - 1);
else if (*(resultsCopy.rbegin()) == '"')
    resultsCopy = resultsCopy.substr(0, resultsCopy.length() - 1);

char chars[] = "\\";
for (unsigned int i = 0; i < strlen(chars); ++i){
    resultsCopy.erase (std::remove(resultsCopy.begin(), resultsCopy.end(), chars[i]), resultsCopy.end());
}

That stuff removes every \ and the " at the beginning and the end. And now it is a valid JSON

Thanks to @Mooing Duck for this

Here is the clean JSON

{"results": [{"sku": "peds-std-us", "url": "https://d2y247jlvj8usn.cloudfront.net/1/2014.06.12/upload test.xlsx", "version": "2014.06.12"}]}

Any advice is welcome

Claudiordgz
  • 3,023
  • 1
  • 21
  • 48