[Twisted-Python] question : json.loads failed using LineReceiver data

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Mon May 14 08:47:17 EDT 2012


On 08:53 am, bino at indoakses-online.com wrote:
>Dear All ..
>
>I'm trying to received json formated text using LineReceiver
>
>Here is my code
>----START----
>class ArduinoReceiver(LineReceiver):
>     def __init__(self):
>         print 'INIT : ArduinoReceiver'
>
>     def lineReceived(self, line):
>         line = line.strip()
>         print 'Received line = '+line
>         if 'Arduino' in line :
>             return
>         self.linedict=json.loads(line)
>
>----STOP-----
>but I got error msg in the log as :
>----START----
>2012-05-14 15:42:00+0700 [-] Received line =
>'{"a_flw_fwd":12,"a_flw_rtr":5029,"a_rpm":519,"b_flw_fwd":23,"b_flw_rtr":8,"b_rpm":913}'

Read your log message.  This is not a valid JSON string:

'{"a_flw_fwd":12,"a_flw_rtr":5029,"a_rpm":519,"b_flw_fwd":23,"b_flw_rtr":8,"b_rpm":913}'

Presumably your objective is to receive something like this instead:

{"a_flw_fwd":12,"a_flw_rtr":5029,"a_rpm":519,"b_flw_fwd":23,"b_flw_rtr":8,"b_rpm":913}

The problem is unrelated to Twisted or the console.  The sender is 
sending the wrong data, or else your parsing of it is incorrect.

Jean-Paul



More information about the Twisted-Python mailing list