json这个小朋友熟悉又陌生,今天给同学们好好讲讲QT是如何使用json的,一句话:简单

1、什么是json?

A:json就是个<key,value>字符串

①一个json对象

{"name":"xupeidong","age":"18"}

②一个json数组里面嵌套json对象

[
{"price": "1", "name": "1", "id": "1"},
{"price": "2", "name": "2", "id": "2"},
{"price": "3", "name": "3", "id": "3"}
]

2、QT里面如何使用json

#include <QJsonDocument>   //解析类,用于将json转换为QByteArrar,或从QByteArray解析出json
#include <QJsonArray> // 封装json数组:["1","2","3"]
#include <QJsonObject> // 封装json对象:{"name": "111","pass": 222}
#include <QJsonParseError> //错误类

①json对象

组装:

QJsonObject json;
json.insert("name", "111");
json.insert("pass", 222);
QJsonDocument doc;
doc.setObject(json);
QByteArray byte = document.toJson(QJsonDocument::Compact);

解析:

QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(byte, &error);
if(error.error == QJsonParseError::NoError)
{
if(doc.isObject())
{
QJsonObject obj = doc.object();
if(obj.contains("name"))
    {
       QJsonValue value = obj.take("name");
       qDebug() << value.toString();
    }
}
}

②json数组

组装:

格式:"[\"000\",\"111\"]"
QJsonArray json;
json.insert(0, "000");
json.insert(1, "111");
QJsonDocument doc;
doc.setArray(json);
QByteArray byte = doc.toJson(QJsonDocument::Compact);
还可以插入json对象:
格式:"[{\"0\":\"000\"},{\"1\":\"111\"}]"
QJsonArray json;
QJsonObject obj;
obj.insert("0","000");
json.insert(0, obj);
QJsonObject obj1;
obj1.insert("1","111");
json.insert(1, obj1);
QJsonDocument doc;
doc.setArray(json);
QByteArray byte = doc.toJson(QJsonDocument::Compact);

解析:

QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(byte, &error);
if(error.error == QJsonParseError::NoError)
{
if(doc.isArray())
{
QJsonArray array = doc.array();
for(int i=0; i<array.size();i++)
{
QJsonValue value = array.at(i);
}
}
}

QT解析和组装json的更多相关文章

  1. java解析和组装json以及一些方法的理解

    这是一个json格式的字符串 第一种情况(简单格式) String result = "{\"name\":\"小明\",\"age\&qu ...

  2. Qt平台下使用QJson解析和构建JSON字符串

    前言 上一篇介绍了C语言写的JSON解析库cJSON的使用:使用cJSON库解析和构建JSON字符串 本篇文章介绍,Qt开发环境下QJson库的使用示例,JSON解析配合API接口,就可以实现一些有趣 ...

  3. 使用cJSON库解析和构建JSON字符串

    使用cJSON库解析和构建JSON字符串 前言 其实之前的两篇博文已经介绍了json格式和如何使用cJSON库来解析JSON: 使用cJSON库解析JSON JSON简介 当时在MCU平台上使用时,会 ...

  4. Python解析非标准JSON(Key值非字符串)

    采集数据的时候经常碰到一些JSON数据的Key值不是字符串,这些数据在JavaScript的上下文中是可以解析的,但在Python中,没有该部分数据的上下文,无法采用json.loads(JSON)的 ...

  5. Gson解析复杂的json数据

    最近在给公司做一个直播APK的项目,主要就是通过解析网络服务器上的json数据,然后将频道地址下载下来再调用Android的播放器进行播放,原先本来打算使用普通的json解析方法即JsonObject ...

  6. 使用Gson解析复杂的json数据

    Gson解析复杂的json数据 最近在给公司做一个直播APK的项目,主要就是通过解析网络服务器上的json数据,然后将频道地址下载下来再调用Android的播放器进行播放,原先本来打算使用普通的jso ...

  7. C# 解析嵌套的json文件.

    概述 今天我同学问我如何转换json文件,没处理过,网上搜了一下,json转excel的很少,反过来倒是有许多人写了工具. json文件的结构大致是这样的: {, , }, , "type& ...

  8. 如何利用.Net内置类,解析未知复杂Json对象

    如何利用.Net内置类,解析未知复杂Json对象 如果你乐意,当然可以使用强大的第三方类库Json.Net中的JObject类解析复杂Json字串 . 我不太希望引入第三方类库,所以在.Net内置类J ...

  9. JSON在线解析,新版本JSON在线解析

    SOJSON,出了新版本的JSON在线解析,真的很好用,可以上下版本.左右版本.效果图如下.它的网址是:http://www.sojson.com/simple_json.html SOJSON集成了 ...

随机推荐

  1. [Swift]LeetCode99. 恢复二叉搜索树 | Recover Binary Search Tree

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  2. [Swift]LeetCode293. 翻转游戏 $ Flip Game

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

  3. [Swift]LeetCode576. 出界的路径数 | Out of Boundary Paths

    There is an m by n grid with a ball. Given the start coordinate (i,j) of the ball, you can move the ...

  4. [Swift]LeetCode653. 两数之和 IV - 输入 BST | Two Sum IV - Input is a BST

    Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...

  5. [Swift]LeetCode728. 自除数 | Self Dividing Numbers

    A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is ...

  6. [Swift]LeetCode830. 较大分组的位置 | Positions of Large Groups

    In a string S of lowercase letters, these letters form consecutive groups of the same character. For ...

  7. HoloLens开发手记- SpectatorView for iOS编译指南

    微软前两天发布了HoloLens 2,给MR开发带来了新的希望,全面的性能和显示效果提升,让人期待. 去年推出的预览版的全新SpectatorView for iOS解决方案,这允许我们直接使用带AR ...

  8. 网络协议 8 - TCP协议(上):性恶就要套路深

    系列文章: 网络协议 1 - 概述 网络协议 2 - IP 是怎么来,又是怎么没的? 网络协议 3 - 从物理层到 MAC 层 网络协议 4 - 交换机与 VLAN:办公室太复杂,我要回学校 网络协议 ...

  9. VR开发相关资源

    1. VRTK(StreamVR TOOLKIT):https://github.com/thestonefox/VRTK 2. StrangeIoc 专为c#和Unity写的框架 3. Frame ...

  10. 什么是Web Server

    WebService到底是什么? 一言以蔽之:WebService是一种跨编程语言和跨操作系统平台的远程调用技术. WebService平台技术 XML+XSD,SOAP和WSDL就是构成WebSer ...