Problems with DzJsonReader

kabexefkabexef Posts: 75

I try to use the DzJsonReader to parse a JSON-String. Therefore I wrote a jsonInterpreter which does nothing but output the called method and return true for each method.

const QString jsonString = "{ \"translations\": [{ \"detected_source_language\":\"EN\", \"text\":\"Hallo, Welt!\" }] }";QByteArray* byteArray = new QByteArray();byteArray->append(jsonString);QBuffer* buffer = new QBuffer(byteArray);outputWindow->clear();outputWindow->append(QString(*byteArray));buffer->open(QBuffer::ReadOnly);DzJsonReader* jsonReader = new DzJsonReader(buffer);KbxfJsonInterpreter* jsonInterpreter = new KbxfJsonInterpreter(outputWindow);DzError error = jsonReader->read(jsonInterpreter);outputWindow->append(QString("JSON-Reader result: %1").arg(getErrorMessage(error)));

Here is the code again, in case the source window is not well readable

// ========================

const QString jsonString = "{ \"translations\": [{ \"detected_source_language\":\"EN\", \"text\":\"Hallo, Welt!\" }] }";

QByteArray* byteArray = new QByteArray();
byteArray->append(jsonString);

QBuffer* buffer = new QBuffer(byteArray);
outputWindow->clear();
outputWindow->append(QString(*byteArray));

buffer->open(QBuffer::ReadOnly);
DzJsonReader* jsonReader = new DzJsonReader(buffer);
KbxfJsonInterpreter* jsonInterpreter = new KbxfJsonInterpreter(outputWindow);
DzError error = jsonReader->read(jsonInterpreter);
outputWindow->append(QString("JSON-Reader result: %1").arg(getErrorMessage(error)));

// ========================

The output I get is ...

{ "translations": [{ "detected_source_language":"EN", "text":"Hallo, Welt!" }] }
startObject(bool) = 1
skipObject
finishObject
JSON-Reader result: No error

... and I don't got any idea why i get no content (no addMember or addItem call) to the interpreter.

Any suggestions?

Post edited by kabexef on

Comments

  • vectorinusvectorinus Posts: 120
    edited February 2022

    Maybe you need to use functions  parse( )parseObject( );  parseArray( );  parseValue( const QString &memberName = QString::null )parseString( QString &str ) or parseFragment( const char *str )  to get the corresponding content?

    Post edited by vectorinus on
  • vectorinus said:

    Maybe you need to use functions  parse( )parseObject( );  parseArray( );  parseValue( const QString &memberName = QString::null )parseString( QString &str ) or parseFragment( const char *str )  to get the corresponding content?

    Thanks for response but the mentioned methods are private in DzJsonReader so I can't use them explicitely.

  • DobitDobit Posts: 181

    It's been a while, but I still want to give a quick feedback. I tested the example and it seems to me that DzJsonReader needs multiline JSON. When I put newlines in the JSON example it works.

  • kabexefkabexef Posts: 75

    Dobit said:

    It's been a while, but I still want to give a quick feedback. I tested the example and it seems to me that DzJsonReader needs multiline JSON. When I put newlines in the JSON example it works.

    Thanks, I'll try it.

Sign In or Register to comment.