Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用PHP解析JSON数组输出_C++_Arrays_Json_Qt - Fatal编程技术网

C++ 用PHP解析JSON数组输出

C++ 用PHP解析JSON数组输出,c++,arrays,json,qt,C++,Arrays,Json,Qt,我有一个PHP返回的JSON数组,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",

我有一个PHP返回的JSON数组,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"
  }
]
我尝试了几种方法,我尝试了迭代器,我尝试了从JSonObject数组,但似乎没有任何效果! 到目前为止,我有以下示例代码:

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();
QJsonDocument-jsonResponse=QJsonDocument::fromJson(JData.toUtf8());
QJsonObject jsonObject=jsonResponse.object();
对于(QJsonObject::迭代器it=jsonObject.begin();it!=jsonObject.end();++it){
QJsonArray数组=(*it).toArray();
foreach(常量QJsonValue&v,数组)

qDebug()刚刚通过先将数据放入QString验证了您的代码(并不意味着需要这样做,只是为了我的调试)。发现了几个问题:

  • 数组位于文档的上层
  • QJsonValue::toString()无法遍历嵌套结构,但QJsonValue (变量v)仍然可以通过qDebug()流打印

  • QString JsonStr=
    "["
    "{"
    “uid\:\“1\”
    “\”用户名\“:\”迈克\“,”
    “创建时间”:“2014-12-27 15:30:03”
    ““更新时间”:“2014-12-27 15:30:03”
    ““到期时间”:“2014-12-27 15:30:03”
    "},"
    "{"
    “uid\:\“2\”
    “\”用户名\“:\”杰森\“,”
    “创建时间”:“2014-12-27 15:31:41”
    ““更新时间”:“2014-12-27 15:31:41”
    ““到期时间”:“2014-12-27 15:31:41”
    "},"
    "{"
    “uid\:\“3\”
    “\”用户名\“:\”大卫\“
    “创建时间”:“2014-12-27 18:10:53”
    ““更新时间”:“2014-12-27 18:10:53”
    ““到期时间”:“2014-12-27 18:10:53”
    "}"
    "]";
    QJsonDocument jsonResponse=QJsonDocument::fromJson(JsonStr.toUtf8());
    QJsonArray数组=jsonResponse.array();
    //印刷品
    foreach(常量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"];
    }