Results 1 to 9 of 9

Thread: QRegExp question for JSON

  1. #1
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QRegExp question for JSON

    Hi,

    I'm trying to use a QRegExp as a quick way to find a value in a simple JSON string. Unfortunately I'm not so familiar with regular expressions and the examples I find on the internet don't seem to work with QRegExp.

    I have a message like below and I want the 'dev' field :
    {"dev":"RSNGSM32", "cmd":"setVoltage", "voltage":7.23}

    I tried this :
    Qt Code:
    1. QRegExp reFindName( "\"dev\":\"(.+?)\"" );
    2. if( reFindName.indexIn( sMessage ) >= 0 )
    3. {
    4. QString sEqName = reFindName.cap(0);
    5. qDebug() << "device name is" << sEqName;
    6. }
    To copy to clipboard, switch view to plain text mode 

    But it doesn't work. I read the documentation on QRegExp and fooled around with the expression, but got nothing that works.

    Any advice would be appreciated.
    Best regards,
    Marc

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QRegExp question for JSON

    A couple of minor changes should produce what you want:
    Qt Code:
    1. //QRegExp reFindName( "\"dev\":\"(.+?)\"" );
    2. QRegExp reFindName( "\"dev\":\"(.*)\"" );
    3. reFindName.setMinimal(true);
    4. if( reFindName.indexIn( sMessage ) >= 0 )
    5. {
    6. //QString sEqName = reFindName.cap(0);
    7. QString sEqName = reFindName.cap(1);
    8. qDebug() << "device name is" << sEqName;
    9. }
    To copy to clipboard, switch view to plain text mode 
    BTW, there is a good regex tester under ../examples/tools/regex

    HTH
    Last edited by norobro; 12th January 2011 at 04:26. Reason: Forgot setMinimal() statement

  3. The following user says thank you to norobro for this useful post:

    marcvanriet (12th January 2011)

  4. #3
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QRegExp question for JSON

    Hi, I think solution above captures too much. So try like that (it will catch in group nr 1 only: RSNGSM32
    Qt Code:
    1. QRegExp reFindName( "\"dev\": *\"(\\w*)\"" );
    To copy to clipboard, switch view to plain text mode 

    If you need spaces in device name then:
    Qt Code:
    1. QRegExp reFindName( "\"dev\": *\"([\\w ]*)\"" );
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to MarekR22 for this useful post:

    marcvanriet (12th January 2011)

  6. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QRegExp question for JSON

    Quote Originally Posted by MarekR22 View Post
    Hi, I think solution above captures too much.
    Then you might want to check it. Because the solution works perfekt. (if you set minimal matching to true). And to your solution, what do you do when the device name also have a :, or a !, or a $ or a =, ar a... So this will be more flexible (even escaped " will be allowed):
    Qt Code:
    1. QRegExp reFindName( "\"dev\":\"(((\\\\\")|[^\"])*)\"" );
    To copy to clipboard, switch view to plain text mode 

  7. The following user says thank you to Lykurg for this useful post:

    marcvanriet (12th January 2011)

  8. #5
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QRegExp question for JSON

    Hi All,

    All your solutions work for my application (the device names are rather simple, but nice to know that something exotic won't give unexpected results).
    Qt Code:
    1. "\"dev\": *\"(\\w*)\""
    2. "\"dev\": *\"([\\w ]*)\""
    3. "\"dev\":\"(((\\\\\")|[^\"])*)\""
    To copy to clipboard, switch view to plain text mode 

    Jeeezus.... looks more like ASCII art then code :-)

    Regards,
    Marc

  9. #6
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: QRegExp question for JSON

    I am not at all familiar with JSON but I am quite familiar with tasks of this general nature. Purely for the sake of discussion, are non-regex solutions feasible? For example, if the example string you gave can be taken as representative, then could you get so-called key-value pairs by splitting on "," and then parse each pair in turn by splitting on ":" and testing the key for equality with the string "dev"? The associated value yields the answer.

  10. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QRegExp question for JSON

    And what you want to do if a value or a key of that string contains a ","? That's the benefit of enclosing values in quotes. Also, there is not really a speed difference since the regular expressions are perfectly optimized. And using regexp, you have more power, you are more flexible and you have to maintain less/less "complex" code.

  11. #8
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QRegExp question for JSON

    Hi,
    Enclosed special characters are indeed something that prevent using a simple split(). But it would be quite easy to write a simple parser that processes the JSON string, handles the special characters, and adds all that it encounters onto a key+value list of some sort. RegEx magic requires less coding though. And others will promote awk or similar tools.

    I'm using QJSON by the way for further processing of the message. But I first needed to know which device the message is intended for, in order to call the corresponding message handler.

    Regards,
    Marc

  12. #9
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: QRegExp question for JSON

    And what you want to do if a value or a key of that string contains a ","?
    Well, it's generally best to "know your data" when formulating a regex too. If the data fields of interest are expected to contain meta-characters that are elsewhere used as delimiters, then of course you can't split() on those characters. On the other hand (and this is something only the OP could answer) will your data ever contain delimiter-characters? If not, then this isn't a problem.

    And using regexp...you have to maintain less/less "complex" code.
    Fewer lines, sure. Lower complexity? I suppose that's a matter of opinion. Be kind to your maintenance programmer who may or may not be yourself.

Similar Threads

  1. JSON - parsing (arrays)
    By Tomasz in forum Newbie
    Replies: 2
    Last Post: 29th December 2010, 14:08
  2. parsing JSON format
    By Raajesh in forum Qt Programming
    Replies: 5
    Last Post: 12th September 2010, 00:40
  3. Qt and JSON
    By Chiggins in forum Qt Programming
    Replies: 2
    Last Post: 28th June 2010, 14:12
  4. Build json, how to?
    By martinn in forum Qt Programming
    Replies: 5
    Last Post: 14th March 2010, 16:19
  5. json: eval
    By mickey in forum General Programming
    Replies: 3
    Last Post: 17th January 2008, 20:50

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.