Results 1 to 5 of 5

Thread: JSON Post Help

  1. #1
    Join Date
    Apr 2015
    Posts
    20
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default JSON Post Help

    {
    "request":"senddata",
    "datatype" : "event",
    "id" : "123123",
    "eventKey":"High Temperature",
    "timestamp":"2017-01-13T20:15:57Z",
    "eventParameters":{
    "Temperature": 25,
    "High Temperature": 35,
    "Setpoint": 25,
    }
    }

    For the above JSON, I've tried to use a QJsonDocument object like this:

    Qt Code:
    1. QByteArray arr = "{\"request\" : \"senddata\","
    2. "\"datatype\" : \"event\","
    3. "\id\" : \"123123\","
    4. "\"eventKey\" : \"High Temperature\","
    5. "\timestamp\" : \"";
    6. arr + dt;
    7. arr += "\","
    8. "\eventParameters\" : {"
    9. "\"Temperature\" : 25,"
    10. "\"High Temperature\" : 30,"
    11. "\"Setpoint\" : 25}}";
    12. doc = QJsonDocument::fromJson(arr);
    To copy to clipboard, switch view to plain text mode 

    I need this because the timestamp key is a variable. I can't get this to work properly. I've also tried using the insert method with QJsonObjec more like:

    Qt Code:
    1. json.insert("id","070707");
    2. json.insert("request","senddata");
    3. json.insert("datatype","event");
    4. json.insert("eventKey","High Temperature");
    5. json.insert("timestamp", QDateTime::currentDateTime().toTimeSpec(Qt::OffsetFromUTC).toString(Qt::ISODate));
    6. ...
    To copy to clipboard, switch view to plain text mode 

    No joy here either. So my real question is how to prepare the nested JSON example for a REST type POST command. It would also be great to see a JSON array example.

    Thanks,
    -Rich

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: JSON Post Help

    Quote Originally Posted by rhb327 View Post
    ... No joy here either....
    What is the problem? The code looks fine.

    Try running this.
    Qt Code:
    1. QJsonDocument doc;
    2.  
    3. QJsonObject json;
    4.  
    5. json.insert("id","070707");
    6. json.insert("request","senddata");
    7. json.insert("datatype","event");
    8. json.insert("eventKey","High Temperature");
    9. json.insert("timestamp", QDateTime::currentDateTime().toTimeSpec(Qt::OffsetFromUTC).toString(Qt::ISODate));
    10.  
    11. doc.setObject(json);
    12.  
    13. std::cout << doc.toJson(QJsonDocument::Indented).toStdString();
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Apr 2015
    Posts
    20
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: JSON Post Help

    I can't seem to get the object within and object part right:
    {
    ...
    "eventParameters":{
    "Temperature": 25,
    "High Temperature": 35,
    "Setpoint": 25
    }

    }

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: JSON Post Help

    Will look trivial
    Qt Code:
    1. QDateTime date = QDateTime::currentDateTime().toTimeSpec(Qt::OffsetFromUTC);
    2. int temperature = 25;
    3. int highTemperature = 35;
    4. int setpoint = 25;
    5.  
    6. QJsonObject event;
    7. event["Temperature"] = temperature;
    8. event["High Temperature"] = highTemperature;
    9. event["Setpoint"] = setpoint;
    10.  
    11. QJsonObject json;
    12. json["id"] = "070707";
    13. json["request"] = "senddata";
    14. json["datatype"] = "event";
    15. json["eventKey"] = "High Temperature";
    16. json["timestamp"] = date.toString(Qt::ISODate);
    17. json["eventParameters"] = event;
    18.  
    19. QJsonDocument doc;
    20. doc.setObject(json);
    21.  
    22. std::cout << doc.toJson(QJsonDocument::Indented).toStdString();
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    Apr 2015
    Posts
    20
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: JSON Post Help

    I see...so a QJsonObject can be assigned to an element of another QJsonObject. Thanks!

Similar Threads

  1. Post JSON request in Qt 4.7
    By haxuanthao in forum Newbie
    Replies: 1
    Last Post: 2nd November 2016, 09:18
  2. Sending Json post request in 5.6
    By d1psy in forum Qt Programming
    Replies: 4
    Last Post: 24th August 2016, 20:59
  3. Correct POST html with JSON data
    By Mbded in forum Qt Programming
    Replies: 11
    Last Post: 23rd August 2015, 11:19
  4. Send JSON & Key via HTTP POST
    By Poonarge in forum Newbie
    Replies: 3
    Last Post: 21st August 2014, 10:13
  5. Replies: 0
    Last Post: 12th June 2014, 13:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.