代码拉取完成,页面将自动刷新
#include "jsonanalysis.h"
JsonAnalysis::JsonAnalysis()
{
}
QByteArray JsonAnalysis::Analysis(JsonProType_t T,QByteArray Data)
{
QByteArray Result;
if(T == BB_CLOUD_CMD)
{
Result = BBCloudCmdAnalysis(Data);
}
return Result;
}
QByteArray JsonAnalysis::BBCloudCmdAnalysis(QByteArray Data)
{
QJsonParseError err;
QJsonDocument doc = QJsonDocument::fromJson(Data,&err);
if(err.error != QJsonParseError::NoError){
qDebug() << err.errorString();
}
QJsonDocument ResJson;
QJsonObject Object;
if(doc.isNull())
{
BBCloudResult(Object,0,1,"Json format error");
}
else
{
//int cmd = doc["cmd"].toInt();
int cmd = GetCmd(doc.object());
if(cmd == 0) // read request
{
QJsonArray obj;
QString err;
int code = 0;
if(GetData(doc.object(),obj,err)){
code = BBCloudCmdRead(obj,err);
if(code == obj.count()){
code = 1;
}else if(code != 0){
code = 2;
}
}else {
code = 1;
}
BBCloudResult(Object,cmd,code,err);
BBCloudAddDataResult(Object,obj);
}
else if(cmd == 1) // write request
{
QJsonArray obj = doc.object().value("data").toArray();
QString err;
int code = 0;
if(GetData(doc.object(),obj,err)){
code = BBCloudCmdWrite(obj,err);
if(code == obj.count()){
code = 1;
}else if(code != 0){
code = 2;
}
}else {
code = 1;
}
BBCloudResult(Object,cmd,code,err);
BBCloudAddDataResult(Object,obj);
}
else if(cmd == -1)
{
BBCloudResult(Object,cmd,1,"command lose");
}
else
{
BBCloudResult(Object,cmd,1,"Unsupported command");
}
}
ResJson.setObject(Object);
return ResJson.toJson(QJsonDocument::Compact);
}
int JsonAnalysis::BBCloudCmdRead(QJsonArray &Data,QString &errMsg)
{
int Result = 0;
for(int i = 0;i < Data.count();i++)
{
QJsonObject ob = Data[i].toObject();
QString err;
if(m_JsonKeyToSys.Read(ob,err) == false)
{
errMsg += err;
Result++;
}
Data[i] = ob;
}
return Result;
}
int JsonAnalysis::BBCloudCmdWrite(QJsonArray Data,QString &errMsg)
{
int Result = 0;
for(QJsonArray::Iterator it = Data.begin(); it != Data.end(); it++)
{
QJsonObject ob = it[0].toObject();
QString err;
if(m_JsonKeyToSys.Write(ob,err) == false)
{
errMsg += err;
Result++;
}
}
m_JsonKeyToSys.WriteReset();
return Result;
}
void JsonAnalysis::BBCloudResult(QJsonObject &Ob,int cmd,int code,QString Msg)
{
Ob.insert("cmd",cmd);
Ob.insert("code",code);
Ob.insert("msg",Msg);
}
void JsonAnalysis::BBCloudAddDataResult(QJsonObject &Ob,QJsonArray Arr)
{
Ob.insert("data",Arr);
}
int JsonAnalysis::GetCmd(QJsonObject Ob)
{
if(Ob.contains("cmd") && Ob.value("cmd").isDouble())
{
return Ob.value("cmd").toInt();
}
return -1;
}
bool JsonAnalysis::GetData(QJsonObject Ob,QJsonArray &Arr,QString &err)
{
if(Ob.contains("data") && Ob.value("data").isArray())
{
QJsonValue V = Ob.take("data");
Arr = V.toArray();
return true;
}
err = "data array format error";
return false;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。