解析 PHP 输出的 JSON 数组
Parse JSON array output by PHP
我有一个 JSON 数组,PHP 返回,PHP 的示例输出:
[
{
"uid": "1",
"username": "mike",
"time_created": "2014-12-27 15:30:03",
"time_updated": "2014-12-27 15:30:03",
"time_expires": "2014-12-27 15:30:03"
},
{
"uid": "2",
"username": "jason",
"time_created": "2014-12-27 15:31:41",
"time_updated": "2014-12-27 15:31:41",
"time_expires": "2014-12-27 15:31:41"
},
{
"uid": "3",
"username": "david",
"time_created": "2014-12-27 18:10:53",
"time_updated": "2014-12-27 18:10:53",
"time_expires": "2014-12-27 18:10:53"
}
]
我尝试了几种方法,我尝试了 Iterator,我尝试了 JSonObject 的 toArray,但似乎没有任何效果!
到目前为止,我有这个示例代码:
QJsonDocument jsonResponse = QJsonDocument::fromJson(JData.toUtf8());
QJsonObject jsonObject = jsonResponse.object();
for (QJsonObject:: Iterator it = jsonObject.begin(); it != jsonObject.end(); ++it) {
QJsonArray array= (*it).toArray();
foreach (const QJsonValue & v, array)
qDebug() << v.toString();
我尝试了其他几种方法,但没有成功。我需要遍历此 JSON 数据。请指教。我正在使用 QT 5.4,C++。
刚刚通过先将数据放入 QString 来验证您的代码(并不意味着需要这样做,仅用于我的调试)。发现了几个问题:
- 数组位于文档的上层。
- QJsonValue::toString() 无法遍历嵌套结构,但是 QJsonValue
(变量 v)仍然可以通过 qDebug() 流打印。
QString JsonStr =
"["
"{"
"\"uid\": \"1\","
"\"username\": \"mike\","
"\"time_created\": \"2014-12-27 15:30:03\","
"\"time_updated\": \"2014-12-27 15:30:03\","
"\"time_expires\": \"2014-12-27 15:30:03\""
"},"
"{"
"\"uid\": \"2\","
"\"username\": \"jason\","
"\"time_created\": \"2014-12-27 15:31:41\","
"\"time_updated\": \"2014-12-27 15:31:41\","
"\"time_expires\": \"2014-12-27 15:31:41\""
"},"
"{"
"\"uid\": \"3\","
"\"username\": \"david\","
"\"time_created\": \"2014-12-27 18:10:53\","
"\"time_updated\": \"2014-12-27 18:10:53\","
"\"time_expires\": \"2014-12-27 18:10:53\""
"}"
"]";
QJsonDocument jsonResponse = QJsonDocument::fromJson(JsonStr.toUtf8());
QJsonArray array = jsonResponse.array();
// print
foreach (const QJsonValue & v, array)
{
qDebug() << v;
}
// or parse
foreach (const QJsonValue& v, array)
{
QJsonObject o = v.toObject();
qDebug() << o["username"];
qDebug() << o["time_expires"];
}
我有一个 JSON 数组,PHP 返回,PHP 的示例输出:
[
{
"uid": "1",
"username": "mike",
"time_created": "2014-12-27 15:30:03",
"time_updated": "2014-12-27 15:30:03",
"time_expires": "2014-12-27 15:30:03"
},
{
"uid": "2",
"username": "jason",
"time_created": "2014-12-27 15:31:41",
"time_updated": "2014-12-27 15:31:41",
"time_expires": "2014-12-27 15:31:41"
},
{
"uid": "3",
"username": "david",
"time_created": "2014-12-27 18:10:53",
"time_updated": "2014-12-27 18:10:53",
"time_expires": "2014-12-27 18:10:53"
}
]
我尝试了几种方法,我尝试了 Iterator,我尝试了 JSonObject 的 toArray,但似乎没有任何效果! 到目前为止,我有这个示例代码:
QJsonDocument jsonResponse = QJsonDocument::fromJson(JData.toUtf8());
QJsonObject jsonObject = jsonResponse.object();
for (QJsonObject:: Iterator it = jsonObject.begin(); it != jsonObject.end(); ++it) {
QJsonArray array= (*it).toArray();
foreach (const QJsonValue & v, array)
qDebug() << v.toString();
我尝试了其他几种方法,但没有成功。我需要遍历此 JSON 数据。请指教。我正在使用 QT 5.4,C++。
刚刚通过先将数据放入 QString 来验证您的代码(并不意味着需要这样做,仅用于我的调试)。发现了几个问题:
- 数组位于文档的上层。
- QJsonValue::toString() 无法遍历嵌套结构,但是 QJsonValue (变量 v)仍然可以通过 qDebug() 流打印。
QString JsonStr =
"["
"{"
"\"uid\": \"1\","
"\"username\": \"mike\","
"\"time_created\": \"2014-12-27 15:30:03\","
"\"time_updated\": \"2014-12-27 15:30:03\","
"\"time_expires\": \"2014-12-27 15:30:03\""
"},"
"{"
"\"uid\": \"2\","
"\"username\": \"jason\","
"\"time_created\": \"2014-12-27 15:31:41\","
"\"time_updated\": \"2014-12-27 15:31:41\","
"\"time_expires\": \"2014-12-27 15:31:41\""
"},"
"{"
"\"uid\": \"3\","
"\"username\": \"david\","
"\"time_created\": \"2014-12-27 18:10:53\","
"\"time_updated\": \"2014-12-27 18:10:53\","
"\"time_expires\": \"2014-12-27 18:10:53\""
"}"
"]";
QJsonDocument jsonResponse = QJsonDocument::fromJson(JsonStr.toUtf8());
QJsonArray array = jsonResponse.array();
// print
foreach (const QJsonValue & v, array)
{
qDebug() << v;
}
// or parse
foreach (const QJsonValue& v, array)
{
QJsonObject o = v.toObject();
qDebug() << o["username"];
qDebug() << o["time_expires"];
}