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.
-
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
-
@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 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?? -