Problem parsing json
-
wrote on 3 Mar 2023, 07:27 last edited by
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.
-
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.
Lifetime Qt Championwrote on 3 Mar 2023, 07:32 last edited by 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.
-
@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.
wrote on 3 Mar 2023, 07:48 last edited by@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/3