itz
itz

Reputation: 29

JSON Parsing with Nao robot - AttributeError

I'm using a NAO robot with naoqi version 2.1 and Choregraphe on Windows. I want to parse json from an attached file to the behavior. I attached the file like in that link. Code:

def onLoad(self):
    self.filepath = os.path.join(os.path.dirname(ALFrameManager.getBehaviorPath(self.behaviorId)), "fileName.json")
def onInput_onStart(self):
    with open(self.filepath, "r") as f:
        self.data = self.json.load(f.get_Response())
    self.dataFromFile = self.data['value']
    self.log("Data from file: " + str(self.dataFromFile))

But when I run this code on the robot (connected with a router) I'll get an error:

    [ERROR] behavior.box :_safeCallOfUserMethod:281 _Behavior__lastUploadedChoregrapheBehaviorbehavior_1136151280__root__AbfrageKontostand_3__AuslesenJSONDatei_1: Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/naoqi.py", line 271, in _safeCallOfUserMethod
    func()
  File "<string>", line 20, in onInput_onStart
  File "/usr/lib/python2.7/site-packages/inaoqi.py", line 265, in <lambda>
    __getattr__ = lambda self, name: _swig_getattr(self, behavior, name)
  File "/usr/lib/python2.7/site-packages/inaoqi.py", line 55, in _swig_getattr
    raise AttributeError(name)
AttributeError: json

I already tried to understand the code from the correspondending lines but I couldn't fixed the error. But I know that the type of my object f is 'file'. How can I open the json file as a json file?

Upvotes: 0

Views: 264

Answers (1)

Emile
Emile

Reputation: 2971

Your problem comes from this:

self.json.load(f.get_Response())

... there is no such thing as "self.json" on a Choregraphe box, import json and then do json.load. And what is get_Response ? That method doesn't exist on anything in Python that I know of.

You might want to first try making a standalone python script (that doesn't use the robot) that can read your json file before you try it with choregraphe. It will be easier.

Upvotes: 1

Related Questions