星海's Blog

老头初学编程
gsoap 2.8.x 的中文第一字节乱码问题。
Qt源代码初探(一)——qglobal.h

Qt = Cute Great LoveIt 之Json TreeWidget的生成 与QHttpMultiPart

星海 posted @ 2013年4月06日 22:43 in 通用知识 , 2379 阅读

一切都很简单清爽,甚至感觉超过了Python(我不会用PYTHON,但看过相关Python实现)。。。。太爽了。。。呵呵。。。

 

示例1:Json数据解析并在QTreeWidget中直接显示JSON解析后的树

#include "JsonTree.h"
#include <QtCore>

JsonTree::JsonTree(QTreeWidget *widget, const QJsonObject &object)
{
  treeWidget = widget;
  responseObject = object;
  parseObject(responseObject);
}

void JsonTree::parseObject(const QJsonObject &object, QTreeWidgetItem *parent)
{
  QStringList objectKeys = object.keys();
  QTreeWidgetItem *child;
  for (int i = 0; i < objectKeys.size(); i++) {

    if (parent == NULL)
      child = new QTreeWidgetItem(treeWidget);
    else
      child = new QTreeWidgetItem(parent);

    child->setText(0, objectKeys[i]);
    QJsonValue value = object[objectKeys.at(i)];
    parseJsonValue(value, 1, child);
  }
}

void JsonTree::parseJsonValue(const QJsonValue &jsonValue, int column, QTreeWidgetItem *node)
{
  QString valueStr;

  QJsonArray  array;
  switch (jsonValue.type()) {
  case QJsonValue::Bool:
    valueStr = jsonValue.toBool() ? "True" : "False";
    node->setText(column, valueStr);
    break;
  case QJsonValue::String:
    valueStr = jsonValue.toString();
    node->setText(column, valueStr);
    break;
  case QJsonValue::Double:
    valueStr = QString::number(jsonValue.toDouble());
    node->setText(column, valueStr);
    break;
  case QJsonValue::Null:
    valueStr = "NULL";
    node->setText(column, valueStr);
    break;
  case QJsonValue::Undefined:
    qDebug() << "Undefined Json Value, check your request";
    break;
  case QJsonValue::Array:
    // Array used Key(array.at(i)) :null
    array = jsonValue.toArray();
    for (int i = 0; i < array.size(); i++) {
      parseJsonValue(array[i], 0, new QTreeWidgetItem(node));
    }
    break;
  case QJsonValue::Object:
    parseObject(jsonValue.toObject(), node);
    break;
  default:
    qDebug() << "what's the fucking in parseJsonValue default";
    break;
  }
}

 

示例2:当前各种WebService横行,从B2C/B2B到社交网络,普遍要用到POST方法组装参数更新当前数据,Qt提供了强大但方便可操作的QHttpMultiPart。一切都很简单爽快,没有歧义和乱七八糟的Boundary和各种\r\n让你头晕。

  QHttpMultiPart picPost;

  QHttpPart statusPart;
  statusPart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain"));
  statusPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"status\""));
  statusPart.setBody("测试QT5-SINA-WEIBO-API, json Formatter");

  QHttpPart imagePart;
  imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
  imagePart.setHeader(QNetworkRequest::ContentDispositionHeader,
                      QVariant("form-data; name=\"pic\"; filename=\"test.jpg\""));
  QFile *file = new QFile("/home/sd44/test.jpg");
  file->open(QIODevice::ReadOnly);
  imagePart.setBodyDevice(file);

  picPost.append(statusPart);
  picPost.append(imagePart);


  networkAccessManager->post(sinaUploadApiUrl, picPost);



登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter