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. Converting QJsonObject to QJsonArray

Converting QJsonObject to QJsonArray

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 1 Posters 4.3k 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.
  • A Offline
    A Offline
    Anticross
    wrote on 30 Nov 2016, 15:08 last edited by A Former User
    #1

    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();
            }
    A 1 Reply Last reply 30 Nov 2016, 15:58
    0
    • A Anticross
      30 Nov 2016, 15:08

      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();
              }
      A Offline
      A Offline
      Anticross
      wrote on 30 Nov 2016, 15:58 last edited by
      #2

      @Anticross said in Converting QJsonObject to QJsonArray:

      *iter

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Anticross
        wrote on 30 Nov 2016, 16:10 last edited by
        #3

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

        1 Reply Last reply
        0

        1/3

        30 Nov 2016, 15:08

        • Login

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