Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem parsing json
Forum Update on Tuesday, May 27th 2025

Problem parsing json

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 198 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kipalex
    wrote on 3 Mar 2023, 07:27 last edited by
    #1

    Good afternoon!

    It does not work out to the end to disassemble json. json document contains a nested array of channel_ids. but for some reason, this array is always empty.

    File Structure:

    [
      {
        "technology": "PJSIP",
        "resource": "provider-number",
        "state": "online",
        "channel_ids": [
        "167782"
        ]
      },
      {
        "technology": "PJSIP",
        "resource": "201",
        "state": "online",
        "channel_ids": [
        "16778267"
        ]
      },
      {
        "technology": "PJSIP",
        "resource": "101",
        "state": "offline",
        "channel_ids": []
      }
    

    The code itself:

    if(document.isArray()){
    
                QJsonArray main_array = document.array();
    
                // Перебирая все элементы массива
                for(int i = 0; i < main_array.count(); i++){
                    QJsonObject technology = main_array.at(i).toObject();
                    qDebug() << "Используемая технология:" << technology.value("technology").toString();
    
                    QJsonObject resource = main_array.at(i).toObject();
                    qDebug() << "Ресурс:" << resource.value("resource").toString();
    
                    QJsonObject state = main_array.at(i).toObject();
                    qDebug() << "Состояние:" << state.value("state").toString();
    
                    // Забираем из второго массива объект
                    QJsonObject second = main_array.at(i).toObject();
                    // Второе значение пропишем строкой
                    QJsonValue jv_second = second.value("channel_ids");
    
                    if(jv_second.isArray()){
                        // Забираем массив из данного свойства
                        QJsonArray second_array = jv_second.toArray();
                        // Перебирая все элементы массива
                        for(int s = 0; s < second_array.count(); s++){
                            QJsonObject channel_ids = second_array.at(s).toObject();
                            // Забираем значения свойств channel_ids
                            qDebug() << "Канал:" << channel_ids.value("channel_ids").toString();
                            }
                        }
                    }
            }
    

    On the way out, I get:

    Используемая технология: "PJSIP"
    Ресурс: "provider-number"
    Состояние: "online"
    Канал: ""
    Используемая технология: "PJSIP"
    Ресурс: "201"
    Состояние: "online"
    Канал: ""
    Используемая технология: "PJSIP"
    Ресурс: "101"
    Состояние: "offline"
    

    I can't understand why an array (channel_ids) with data turns out to be empty.

    J 1 Reply Last reply 3 Mar 2023, 07:32
    0
    • K kipalex
      3 Mar 2023, 07:27

      Good afternoon!

      It does not work out to the end to disassemble json. json document contains a nested array of channel_ids. but for some reason, this array is always empty.

      File Structure:

      [
        {
          "technology": "PJSIP",
          "resource": "provider-number",
          "state": "online",
          "channel_ids": [
          "167782"
          ]
        },
        {
          "technology": "PJSIP",
          "resource": "201",
          "state": "online",
          "channel_ids": [
          "16778267"
          ]
        },
        {
          "technology": "PJSIP",
          "resource": "101",
          "state": "offline",
          "channel_ids": []
        }
      

      The code itself:

      if(document.isArray()){
      
                  QJsonArray main_array = document.array();
      
                  // Перебирая все элементы массива
                  for(int i = 0; i < main_array.count(); i++){
                      QJsonObject technology = main_array.at(i).toObject();
                      qDebug() << "Используемая технология:" << technology.value("technology").toString();
      
                      QJsonObject resource = main_array.at(i).toObject();
                      qDebug() << "Ресурс:" << resource.value("resource").toString();
      
                      QJsonObject state = main_array.at(i).toObject();
                      qDebug() << "Состояние:" << state.value("state").toString();
      
                      // Забираем из второго массива объект
                      QJsonObject second = main_array.at(i).toObject();
                      // Второе значение пропишем строкой
                      QJsonValue jv_second = second.value("channel_ids");
      
                      if(jv_second.isArray()){
                          // Забираем массив из данного свойства
                          QJsonArray second_array = jv_second.toArray();
                          // Перебирая все элементы массива
                          for(int s = 0; s < second_array.count(); s++){
                              QJsonObject channel_ids = second_array.at(s).toObject();
                              // Забираем значения свойств channel_ids
                              qDebug() << "Канал:" << channel_ids.value("channel_ids").toString();
                              }
                          }
                      }
              }
      

      On the way out, I get:

      Используемая технология: "PJSIP"
      Ресурс: "provider-number"
      Состояние: "online"
      Канал: ""
      Используемая технология: "PJSIP"
      Ресурс: "201"
      Состояние: "online"
      Канал: ""
      Используемая технология: "PJSIP"
      Ресурс: "101"
      Состояние: "offline"
      

      I can't understand why an array (channel_ids) with data turns out to be empty.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 3 Mar 2023, 07:32 last edited by jsulm 3 Mar 2023, 07:32
      #2

      @kipalex said in Problem parsing json:

      QJsonObject channel_ids = second_array.at(s).toObject();

      Your array elements are not objects. So, second_array.at(s).toString() should be what you need.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 1 Reply Last reply 3 Mar 2023, 07:48
      2
      • J jsulm
        3 Mar 2023, 07:32

        @kipalex said in Problem parsing json:

        QJsonObject channel_ids = second_array.at(s).toObject();

        Your array elements are not objects. So, second_array.at(s).toString() should be what you need.

        K Offline
        K Offline
        kipalex
        wrote on 3 Mar 2023, 07:48 last edited by
        #3

        @jsulm
        For sure, thank you very much.
        As a result, my code turned out like this:

        if(jv_second.isArray()){
                            // Забираем массив из данного свойства
                            QJsonArray second_array = jv_second.toArray();
                            // Перебирая все элементы массива
                            for(int s = 0; s < second_array.count(); s++){
                                // QJsonObject channel_ids = second_array.at(s).toObject();
                                QString channel_ids = second_array.at(s).toString();
                                // Забираем значения свойств channel_ids
                                qDebug() << "Канал:" << channel_ids;
                                }
                            }
        

        Maybe someone will benefit.

        1 Reply Last reply
        0

        2/3

        3 Mar 2023, 07:32

        • Login

        • Login or register to search.
        2 out of 3
        • First post
          2/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved