Unsolved How to create special json format to post
-
@ztencmcp said in How to create special json format to post:
QString json_str(byte_array);
isn't that what puts the double-quotes around the whole thing, which is what (I think) you say you so not want? If the server wants an object, which do you convert it to a (JSON) string? (Note that I am not a JSON expert!)
-
Everything seems fine, why do you think it's wrong?
-
-
@JonB Thanks, even if i use QByteArray type, the server responses json format error when i post data with QNetworkAccessManager. But if i use CPPRESTSDK, i can create the right json format with
web::json::value postdata.serialize()
, the right format is:{/"username/": /"admin/", /"password/": /"admin/", /"content/": /"hello world/"}
without qoute before and after brace
-
@ztencmcp said in How to create special json format to post:
{/"username/": /"admin/", /"password/": /"admin/", /"content/": /"hello world/"}
This is no valid json.
-
@VRonin Thanks, if i use CPPRESTSDK, i can create the right json format with
web::json::value postdata.serialize()
, the right format is:{/"username/": /"admin/", /"password/": /"admin/", /"content/": /"hello world/"}
without qoute before and after brace
-
@ztencmcp said in How to create special json format to post:
without qoute before and after brace
Can you please explain how you're printing your JSON, so you see these quotes? Do you use qDebug()?
Because your "right format" you just posted still does contain the quotes... -
@Christian-Ehrlicher This is what i used CPPRESTSDK to build the right format with
web::json::value postdata.serialize()
{/"username/": /"admin/", /"password/": /"admin/", /"content/": /"hello world/"}
-
@ztencmcp can you save the "correct" string to a file and show that one to us?
It's still very confusing, what the actual problem is!
-
@ztencmcp said in How to create special json format to post:
the server responses json format error when i post data with QNetworkAccessManager.
How do you post? Please show the code from document.setObject(json); to
QNetworkAccessManager::post()
-
@ztencmcp
I don't know, I'm not a JSON expert, but like @Christian-Ehrlicher I thought the JSON you show would not be valid. Look at this bit:/"username/": /"admin/"
You have a
/
outside the quoted string for both the property name & value. Is that really correct?? -
you can see the pic, the right one i created it with cpprestsdk web::json::value serialize() fucntion, the wrong one i created it with qt5 QJsonObject。http server only accepts the right one。
-
-
@JKSH
here's the code. Thanks.And i used cpprestsdk to do the same thing and it's right.
-
When you look at the content of the QByteArray
You'll see, there is no " at the beginning or at the end. The Qt creates the same "JSON string" as cpprestsdk
the error is somewhere else.
-
@ztencmcp
Can we please be clear about one thing: in your first code it used/"
, in the stuff you are pasting now it uses\"
. Are you saying that in JSON one can use either/
or\
as the "escape" character?? -
@KroMignon Thanks for advice, pls see the pics below.
-
@ztencmcp You did NOT specify in your POST request the format!
QNetworkRequest request(QUrl("https://example.com")); request.setRawHeader("User-Agent", "My app name v0.1"); request.setRawHeader("X-Custom-User-Agent", "My app name v0.1"); request.setRawHeader("Content-Type", "application/json"); request.setRawHeader("Content-Length", postdata.size()); manager->post(request, postdata);
or
QNetworkRequest request(QUrl("https://example.com")); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); manager->post(request, postdata);
Should do the job!
-
@KroMignon Thanks, let me try.