How to create special json format to post
-
wrote on 19 Nov 2019, 09:24 last edited by ztencmcp
Hi Guys,
There is a server requires json format as below, how can i create it? Thanks.
this is what the server needs : {/"username/": /"admin/", /"password/": /"admin/", /"content/": /"hello world/"}
I create it with QJsonDocument and QJsonObject, it is like this:
"{/"username/":/"admin/", /"password/", /" admin/", /"content/": /"hello world/"}", it doesn't work.QJsonObject json; json.insert("username", "admin"); json.insert("password", "admin"); json.insert("content", "hello world"); QJsonDocument document; document.setObject(json); QByteArray byte_array = document.toJson(QJsonDocument::Compact); QString json_str(byte_array);
Pls help, appreciate it.
-
Hi Guys,
There is a server requires json format as below, how can i create it? Thanks.
this is what the server needs : {/"username/": /"admin/", /"password/": /"admin/", /"content/": /"hello world/"}
I create it with QJsonDocument and QJsonObject, it is like this:
"{/"username/":/"admin/", /"password/", /" admin/", /"content/": /"hello world/"}", it doesn't work.QJsonObject json; json.insert("username", "admin"); json.insert("password", "admin"); json.insert("content", "hello world"); QJsonDocument document; document.setObject(json); QByteArray byte_array = document.toJson(QJsonDocument::Compact); QString json_str(byte_array);
Pls help, appreciate it.
@ztencmcp Please show how you're doing it (show code).
-
wrote on 20 Nov 2019, 08:58 last edited by
Hey Bro,
there's the code, appreciate it.
QJsonObject json; json.insert("username", "admin"); json.insert("password", "admin"); json.insert("content", "hello world"); QJsonDocument document; document.setObject(json); QByteArray byte_array = document.toJson(QJsonDocument::Compact); QString json_str(byte_array);
-
Hey Bro,
there's the code, appreciate it.
QJsonObject json; json.insert("username", "admin"); json.insert("password", "admin"); json.insert("content", "hello world"); QJsonDocument document; document.setObject(json); QByteArray byte_array = document.toJson(QJsonDocument::Compact); QString json_str(byte_array);
wrote on 20 Nov 2019, 09:07 last edited by JonB@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!)
-
wrote on 20 Nov 2019, 09:07 last edited by VRonin
Everything seems fine, why do you think it's wrong?
-
Hey Bro,
there's the code, appreciate it.
QJsonObject json; json.insert("username", "admin"); json.insert("password", "admin"); json.insert("content", "hello world"); QJsonDocument document; document.setObject(json); QByteArray byte_array = document.toJson(QJsonDocument::Compact); QString json_str(byte_array);
wrote on 20 Nov 2019, 13:15 last edited by -
@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!)
wrote on 21 Nov 2019, 02:14 last edited by@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.
-
wrote on 21 Nov 2019, 05:50 last edited by
@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
-
@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... -
@ztencmcp said in How to create special json format to post:
{/"username/": /"admin/", /"password/": /"admin/", /"content/": /"hello world/"}
This is no valid json.
wrote on 21 Nov 2019, 06:05 last edited by@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/"}
-
@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!
-
@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:
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()
-
@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/"}
wrote on 21 Nov 2019, 08:38 last edited by@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?? -
@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... -
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。
wrote on 21 Nov 2019, 09:42 last edited by -
@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()
-
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。
-
wrote on 21 Nov 2019, 09:59 last edited by
@KroMignon Thanks for advice, pls see the pics below.
1/22