×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    159

    Json parsing in Qt

    Hello everyone,
    I am trying to parse the below json value in Qt.

    "{'meta':'200','server_current_time':'2011-09-27 01:22:27','response':{'status':{'success':'Y','name':'dasmohan','first_name':'dasmohan','last_name':'mohandas','userid':'hello@gmail.com','this_id':'396','city':'','gender':'','device_type':'symbian','auth_token':'Y1B75AIS64MRG9DUTZHQVELJW8C2P3FNKO',}}}

    i using the below code for that,

    QJson::Parser parser;

    bool ok;
    ok = true;
    QVariantMap result = parser.parse(QByteArray(txt), &ok).toMap();
    if (!ok) {
    qFatal("An error occurred during parsing");
    exit (1);
    }
    QVariantMap responseMap = result["response"].toMap();
    QVariantMap statusMap = responseMap["status"].toMap();

    qDebug() << "success:" << statusMap["success"].toString();
    qDebug() << "name:" << statusMap["name"].toString();
    qDebug() << "first_name:" << statusMap["first_name"].toString();
    qDebug() << "last_name:" << statusMap["last_name"].toString();
    qDebug() << "userid:" << statusMap["userid"].toString();


    but the value is not even parsed... this is the link i followed. http://qjson.sourceforge.net/usage.html
    bellow is my parsing.h and parsing.cpp file,

    /* This file is part of QJson
    *
    * Copyright (C) 2008 Flavio Castelli <flavio.castelli@gmail.com>
    *
    * This library is free software; you can redistribute it and/or
    * modify it under the terms of the GNU Lesser General Public
    * License version 2.1, as published by the Free Software Foundation.
    *
    *
    * This library is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public License
    * along with this library; see the file COPYING.LIB. If not, write to
    * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    * Boston, MA 02110-1301, USA.
    */

    #include "parser.h"
    #include "parser_p.h"
    #include "json_parser.hh"
    #include "json_scanner.h"

    #include <QtCore/QBuffer>
    #include <QtCore/QStringList>
    #include <QtCore/QTextStream>
    #include <QtCore/QDebug>

    using namespace QJson;

    ParserPrivate::ParserPrivate() :
    m_scanner(0)
    , m_negate(false)
    , m_error(false)
    , m_errorLine(0)
    , m_specialNumbersAllowed(false)
    {
    }

    ParserPrivate::~ParserPrivate()
    {
    delete m_scanner;
    }

    void ParserPrivate::setError(QString errorMsg, int errorLine) {
    m_error = true;
    m_errorMsg = errorMsg;
    m_errorLine = errorLine;
    }

    Parser::Parser() :
    d(new ParserPrivate)
    {
    }

    Parser::~Parser()
    {
    delete d;
    }

    QVariant Parser:arse (QIODevice* io, bool* ok)
    {
    d->m_errorMsg.clear();
    delete d->m_scanner;
    d->m_scanner = 0;

    if (!io->isOpen()) {
    if (!io->open(QIODevice::ReadOnly)) {
    if (ok != 0)
    *ok = false;
    qCritical ("Error opening device");
    return QVariant();
    }
    }

    if (!io->isReadable()) {
    if (ok != 0)
    *ok = false;
    qCritical ("Device is not readable");
    io->close();
    return QVariant();
    }

    d->m_scanner = new JSonScanner (io);
    d->m_scanner->allowSpecialNumbers(d->m_specialNumbersAllowed);
    yy::json_parser parser(d);
    parser.parse();

    delete d->m_scanner;
    d->m_scanner = 0;

    if (ok != 0)
    *ok = !d->m_error;

    io->close();
    return d->m_result;
    }

    QVariant Parser:arse(const QByteArray& jsonString, bool* ok) {
    QBuffer buffer;
    buffer.open(QBuffer::ReadWrite);
    buffer.write(jsonString);
    buffer.seek(0);
    return parse (&buffer, ok);
    }

    QString Parser::errorString() const
    {
    return d->m_errorMsg;
    }

    int Parser::errorLine() const
    {
    return d->m_errorLine;
    }

    void QJson::Parser::allowSpecialNumbers(bool allowSpecialNumbers) {
    d->m_specialNumbersAllowed = allowSpecialNumbers;
    }

    bool Parser::specialNumbersAllowed() const {
    return d->m_specialNumbersAllowed;
    }

    parser.h file

    /* This file is part of QJson
    *
    * Copyright (C) 2008 Flavio Castelli <flavio.castelli@gmail.com>
    *
    * This library is free software; you can redistribute it and/or
    * modify it under the terms of the GNU Lesser General Public
    * License version 2.1, as published by the Free Software Foundation.
    *
    *
    * This library is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    * Lesser General Public License for more details.
    *
    * You should have received a copy of the GNU Lesser General Public License
    * along with this library; see the file COPYING.LIB. If not, write to
    * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    * Boston, MA 02110-1301, USA.
    */

    #ifndef QJSON_PARSER_H
    #define QJSON_PARSER_H

    #include "qjson_export.h"

    class QIODevice;
    class QVariant;

    namespace QJson {

    class ParserPrivate;

    /**
    * @brief Main class used to convert JSON data to QVariant objects
    */
    class QJSON_EXPORT Parser
    {
    public:
    Parser();
    ~Parser();

    /**
    * Read JSON string from the I/O Device and converts it to a QVariant object
    * @param io Input output device
    * @param ok if a conversion error occurs, *ok is set to false; otherwise *ok is set to true.
    * @returns a QVariant object generated from the JSON string
    */
    QVariant parse(QIODevice* io, bool* ok = 0);

    /**
    * This is a method provided for convenience.
    * @param jsonData data containing the JSON object representation
    * @param ok if a conversion error occurs, *ok is set to false; otherwise *ok is set to true.
    * @returns a QVariant object generated from the JSON string
    * @sa errorString
    * @sa errorLine
    */
    QVariant parse(const QByteArray& jsonData, bool* ok = 0);

    /**
    * This method returns the error message
    * @returns a QString object containing the error message of the last parse operation
    * @sa errorLine
    */
    QString errorString() const;

    /**
    * This method returns line number where the error occurred
    * @returns the line number where the error occurred
    * @sa errorString
    */
    int errorLine() const;

    /**
    * Sets whether special numbers (Infinity, -Infinity, NaN) are allowed as an extension to
    * the standard
    * @param allowSpecialNumbers new value of whether special numbers are allowed
    * @sa specialNumbersAllowed
    */
    void allowSpecialNumbers(bool allowSpecialNumbers);

    /**
    * @returns whether special numbers (Infinity, -Infinity, NaN) are allowed
    * @sa allowSpecialNumbers
    */
    bool specialNumbersAllowed() const;

    private:
    Q_DISABLE_COPY(Parser)
    ParserPrivate* const d;
    };
    }

    #endif // QJSON_PARSER_H

    can anyone tell me wat i am going wrong thanks for any replies...

  2. #2
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    India
    Posts
    1,173

    Re: Json parsing in Qt

    what is the problem you are facing?

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    159

    Re: Json parsing in Qt

    Quote Originally Posted by not_dying View Post
    what is the problem you are facing?
    the value which i passed in is not getting parsed... this is how i called the method...

    voidHttpCommunicator::downloadReadyRead() {
    QByteArraybytes=m_networkReply->readAll();//bytes
    JsonObject*layout=newJsonObject;
    QStringjsonData;

    parsing(bytes);


    m_networkReplyBody=m_networkReplyBody+bytes;//string
    if(m_saveResponseToFile){
    QStringlocation=saveFileName(m_url);
    if(saveToFile(bytes)){
    m_networkReplyBody=tr("Filesavedat")+location;
    }
    else{
    m_networkReplyBody=tr("WriteError")+location;
    }
    }
    }

    this is the value of the bytes passed to the code said above

    "{"meta":"200","server_current_time":"2011-10-03 05:54:35","response":{"status":{"success":"Y","name":"hello","first_name":"hello","last_name":"hello","userid":"hello685@gmail.com","id":"419","city":"","gender":"","device_type":"nokia","auth_token":"U4LKEXISV251WHGB783DFMTZAR6QJPN9CY","ProfilePicture":"https:\\some url","sn_id":"null","sn_access_token":"null"}}}"

    in Qjson usage they have said that the value passed is a qstring..

    // json is a QString containing the data to convert
    QVariant result = parser.parse (json, &ok);

    but when i pass the string value to the above method it says

    C:\QtSDK\Examples\sample-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug\..\sample\httpcommunicator.cpp:439: error: no matching function for call to 'QJson::Parser:arse(QString&, bool*)'

    there is no function matching the parameters in the parse class it accept only bytearrays as parameters. tats y i passed the value as qbytearray.

Similar Threads

  1. JSON File parsing
    By tarun.WRT in forum Symbian
    Replies: 2
    Last Post: 2011-06-07, 11:20
  2. Json parsing
    By kalgi in forum Symbian
    Replies: 4
    Last Post: 2011-04-20, 11:42
  3. JSON parsing in QT?
    By sindhuks in forum Qt
    Replies: 7
    Last Post: 2011-03-29, 22:44
  4. How to download JSON ME Api "org.json.me.zip"
    By narendar_discover in forum Mobile Java Networking & Messaging & Security
    Replies: 5
    Last Post: 2010-05-01, 12:15
  5. JSON Parsing
    By raja_salomo in forum Mobile Java Networking & Messaging & Security
    Replies: 4
    Last Post: 2009-09-25, 08:50

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×