Converting QJsonObject to QJsonArray



  • Hi to all. I have some QJsonObject wich has hierarchy inside. I need to convert it to QJsonArray and write to a file. But when I open written file I see array with all 'null' values. What could be wrong ?

    //your code here
    ```QJsonArray ObjectToArray( const QJsonObject &json_obj ) const 
    {
    	QJsonArray json_arr;
    	QJsonObject::const_iterator iter = json_obj.begin();		
    	
    	while (iter != json_obj.end())
    	{
    		iter++;	
    
    		if ( iter->isObject() ) 
    		{
    			json_arr.append(ObjectToArray(iter->toObject()));
    		}
    		else
    		{
    			json_arr.append(*iter);
    		}
    	}
    	
    	return json_arr;
    }
    

    Writing to file like this:
    //your code here

            QFile output_file( file_path );
    	if (output_file.open( QIODevice::WriteOnly) ) {
              const auto json_array= ObjectToArray( some_json_obj);
              output_file.write( QJsonDocument(json_array).toJson( QJsonDocument::Compact) );
              output_file.close();
            }


  • @Anticross said in Converting QJsonObject to QJsonArray:

    *iter

    when I print my *iter in that method with qDebug(), I've got QJasonValue(udifined). Why ?



  • I've got it. The problem was in iterator, actually iter++ must be in the end of loop.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.