Your browser was unable to load all of the resources. They may have been blocked by your firewall, proxy or browser configuration.
Press Ctrl+F5 or Ctrl+Shift+R to have your browser try again.

[Restful][Json format] Help with Json format of our api #4581

ngocanhnu ·

Dear@robinshen @Developers
After upgrade from QB10 to QB12, some api result of Json is not return as we expect.
These api is developed by us, so do not concern about your code.
These api return String of Json format:

{"name": "Mary", "hobbies": ["reading", "swimming"]}

With QB10, some clients of us send connection with header "Accept: application/json", they get the result as we expect:

{"name": "Mary", "hobbies": ["reading", "swimming"]}

But we QB12, with header "Accept: application/json", the get the additon backslashes result:

{\"name\": \"Mary\", \"hobbies\": [\"reading\", \"swimming\"]}

With header not relate "json", everything work fine.
We found that, with QB12 the result is serialized by @writeTo method in JsonProvider class.

We need some advice from yours to solve this issue.
It is the best if you guys can modify some of your code to handle this exception.

  • replies 3
  • views 447
  • stars 0
robinshen ADMIN ·

Official QB12 release returns string in json format without any issues. I just added a test rest method returning a map using your data and I am getting:

{"name": "Mary", "hobbies": ["reading", "swimming"]}
ngocanhnu ·

Unlike your test method, our methods return a Json String:

{"name": "Mary", "hobbies": ["reading", "swimming"]}

So, when user add "application/json" Header, the result above is serialized by JsonProvider.
For the same testing method, you can try change the version of QB in your asset file become the Json String above.
Make get call to "/version" with Accpet "application/json" and "/" to see the different result.

robinshen ADMIN ·

This is actually expected result. Since you are returning a literal string. And when convert this literal string to json, all quotes will be escaped. You will need to return a map in your method like below:

Map<String, Object> data = new HashMap<String, Object>();
data.put("name", "Mary");
List<String> hobbies = new ArrayList<String>();
hobbies.add("reading");
hobbies.add("swimming");
data.put("hobbies", hobbies);
return data