How to create special json format to post
-
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.
-
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);
@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!)
-
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);
-
@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!)
@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
-
@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.
@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/"}
-
@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/"}
@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。
-
@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。
-
@KroMignon Thanks for advice, pls see the pics below.