[Twisted-Python] question : json.loads failed using LineReceiver data
bino oetomo
bino at indoakses-online.com
Mon May 14 02:53:53 MDT 2012
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}'
2012-05-14 15:42:00+0700 [-] Unhandled Error
Traceback (most recent call last):
File
"/usr/local/lib/python2.6/dist-packages/Twisted-11.1.0_r33425-py2.6-linux-i686.egg/twisted/python/log.py",
line 84, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File
"/usr/local/lib/python2.6/dist-packages/Twisted-11.1.0_r33425-py2.6-linux-i686.egg/twisted/python/log.py",
line 69, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File
"/usr/local/lib/python2.6/dist-packages/Twisted-11.1.0_r33425-py2.6-linux-i686.egg/twisted/python/context.py",
line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args,
**kw)
File
"/usr/local/lib/python2.6/dist-packages/Twisted-11.1.0_r33425-py2.6-linux-i686.egg/twisted/python/context.py",
line 81, in callWithContext
return func(*args,**kw)
--- <exception caught here> ---
File
"/usr/local/lib/python2.6/dist-packages/Twisted-11.1.0_r33425-py2.6-linux-i686.egg/twisted/internet/posixbase.py",
line 586, in _doReadOrWrite
why = selectable.doRead()
File
"/usr/local/lib/python2.6/dist-packages/Twisted-11.1.0_r33425-py2.6-linux-i686.egg/twisted/internet/_posixserialport.py",
line 62, in doRead
return fdesc.readFromFD(self.fileno(), self.protocol.dataReceived)
File
"/usr/local/lib/python2.6/dist-packages/Twisted-11.1.0_r33425-py2.6-linux-i686.egg/twisted/internet/fdesc.py",
line 94, in readFromFD
callback(output)
File
"/usr/local/lib/python2.6/dist-packages/Twisted-11.1.0_r33425-py2.6-linux-i686.egg/twisted/protocols/basic.py",
line 564, in dataReceived
why = self.lineReceived(line)
File "./kapal02.tac", line 48, in lineReceived
self.linedict=json.loads(line)
File "/usr/lib/python2.6/json/__init__.py", line 307, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.6/json/decoder.py", line 319, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.6/json/decoder.py", line 338, in raw_decode
raise ValueError("No JSON object could be decoded")
exceptions.ValueError: No JSON object could be decoded
----STOP-----
I try to do json.loads based on received msg using python console, and
it work :
---START---------
bino at bino-desktop:/var/log/rabbitmq$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>>
aa='{"a_flw_fwd":12,"a_flw_rtr":5029,"a_rpm":519,"b_flw_fwd":23,"b_flw_rtr":8,"b_rpm":913}'
>>> mydict=json.loads(aa)
>>> print mydict['a_flw_fwd']
12
---STOP----------
Kindly please tell me why the 'json.loads' is failed in my tac file
while it work in python console.
Sincerely
-bino-
More information about the Twisted-Python
mailing list