QT json返回解析,如何得到结构体中的某个属性

ithiker 2015-09-12 10:40:11
如何解析下面第一个json返回文本:

{
"Customer": "{ “customerId”:”00001”,”customerName”:”gigglesun” }",
"ReturnCode": 200,
"Message": "login success"
}



解析代码:

void NetworkService::replyFinish(QNetworkReply *reply)
{
QString strJsonText = reply->readAll();
QString strText="";
QScriptValue jsontext;
QScriptEngine engineText;
jsontext = engineText.evaluate("value = " + strJsonText);
QScriptValueIterator iteratorText(jsontext);

while (iteratorText.hasNext())
{

iteratorText.next();
QScriptValue entry = iteratorText.value();
qDebug() << entry.toString();
if (iteratorText.name() == "ReturnCode")
{
m_returnCode = iteratorText.value().toInt32();
}
else if (iteratorText.name() == "Message")
{
strText = iteratorText.value().toString();
}
else if (iteratorText.name() == "Customer")
{
m_customId = iteratorText.value().toObject().property(0).toString();
}
}
}


解析中qDebug的输出:

"[object Object]"
"\u5904\u7406\u6210\u529F"
"200"

如何解析得到m_customId,即00001,为什么是"Customer"对应的是"[object Object]",怎样得到00001?,QT新手,求解
...全文
514 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
一去丶二三里 2015-09-16
  • 打赏
  • 举报
回复
ithiker 2015-09-12
  • 打赏
  • 举报
回复

jsonObj["Customer"]..toObject()["CustomerId"]即可
ithiker 2015-09-12
  • 打赏
  • 举报
回复
引用 1 楼 zyq5945 的回复:
看下QJsonDocument的帮助。
用jsonObj["Customer"]获取得太多了,用QJsonArray 时候size又为0?不解,还请版主帮忙看看,谢谢

QJsonDocument jsonResponse = QJsonDocument::fromJson(strJsonText.toUtf8());
QJsonObject jsonObj = jsonResponse.object();

    qDebug()<<jsonObj["Customer"]; 
/*输出QJsonValue(object, QJsonObject({"CreatedById":0,"CreatedOn":"0001-01-01T00:00:00","C
ustomerId":"8e94d585-3c7b-45f3-9d12-f83acf886e3e","CustomerName":"gigglesun","Language":null,"Mobile":"15118123456","ModifiedById":0,"ModifiedOn":"0001-01-01T00:00:00","NickName":null,"Password":null,"SessionKey":"2a1eadf4-5ce4-4308-9530-15967a4baa91","UniqueId":"00000000-0000-0000-0000-000000000000"}))*/


  QJsonArray customerInfoArray = jsonObj["Customer"].toArray();
    qDebug() <<"size = "<< customerInfoArray.size(); // =0
zyq5945 2015-09-12
  • 打赏
  • 举报
回复
看下QJsonDocument的帮助。
彩阳 2015-09-12
  • 打赏
  • 举报
回复
唉,还要toString( )才行啊。
引用 3 楼 gigglesun 的回复:

jsonObj["Customer"]..toObject()["CustomerId"]即可

16,817

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧