SimpleJson Package Installation issue

222 views Asked by At

I’m trying to use simplejson library but i think something went wrong with my installation. it works with some functions for example i’ve tried it with simplejson.dumps and it works fine but when i try simplejson.loads i’m getting an error. I’m not sure if that’s the only thing there is a problem with but it’s the only one i’ve encountered. I’m testing with a simple script that looks like

import simplejson

json_data = {"name": "Jane", "age": 17}
data = simplejson.loads(json_data)

print(type(json_data))
print(type(data))

print(data) 

this is the error im getting

Traceback (most recent call last):

  File "dummy.py", line 4, in <module>

    data = simplejson.loads(json_data)

  File "C:\apps\python25\lib\site-packages\PIL\__init__.py", line 302, in loads



  File "build\bdist.win32\egg\simplejson\decoder.py", line 314, in decode

TypeError: expected string or buffer

i installed with py setup.py install.

Installed c:\apps\python25\lib\site-packages\simplejson-2.0.4-py2.5.egg

Processing dependencies for simplejson==2.0.4

Finished processing dependencies for simplejson==2.0.4

***************************************************************************

WARNING: The C extension could not be compiled, speedups are not enabled.

Plain-Python installation succeeded.

***************************************************************************

I’ve reinstalling it, but it hasn’t worked.

I am restricted to using this specific package and python 2.5.

1

There are 1 answers

0
Lenormju On

There seems to be nothing wrong with your setup, the error is in your code.
SimpleJSON adheres the the same interface than the standard json module.

From the docs of simplejson.loads :

Deserialize s (a str or unicode instance containing a JSON document) to a Python object.

And your exception message is :

TypeError: expected string or buffer

It is because you are giving an object, not the string representation of an object. You already have the result, no need to call the function.
A json library converts between text and object. Here, you already have an object (json_data = {"name": "Jane", "age": 17}).

In other words :

>>> import json
>>> json.loads('{"name": "Jane", "age": 17}')  # loadString
{'name': 'Jane', 'age': 17}  # into an object (a dict)
>>> json.dumps({'name': 'Jane', 'age': 17})  # dumpString
'{"name": "Jane", "age": 17}'  # into a string (representing the object)