1

I have a string returned from python by json.dumps(). I am using python 2.6 or 2.7. When I parse it using JSON.parse() in JavaScript. I got undefined with keys.

My string recieved by javascript(ajax) is looks like:

{'country': u'\u4e0a', 'Search': {'city_num': 25, 'index': ''}, 'Locc': ['116.116', '29.29'], 'cid': '285'}

When I use:

objstr = JSON.parse(jsonstr);
alert(objstr.country);

I got undefined in popup alert window.objstr[0].country and objstr[0]['country'] got undefined either.

What should I do? I have read several article on stackoverflow and google and I still confused about how to solve this problem. Any one can give me some advices? Thanks.

Edit

My codes to receive request from ajax:

  def POST(self):
        resultline = {}
        file = open("..\\cache\\res.cache", 'r')
        last_pos = self.get_pos()
        print last_pos
        pos = int(last_pos)
        file.seek(pos)
        line = file.readline()  
        print line
        if ''== line:
            file.seek(0, 0)
            line = file.readline()
        last_pos = file.tell()
        self.store_pos(last_pos)
        resultline = line
        file.close()
        if '' != resultline:
            resultlinetmp = resultline[0:len(resultline)-1]
        return json.dumps(resultlinetmp)
Nick Dong
  • 3,638
  • 8
  • 47
  • 84
  • `u'\u4e0a'` is causing the issue, there is a `u` outside the single quotes –  May 19 '14 at 05:59
  • 2
    Are you sure you are using `json.dumps()`? That looks like the `repr` of the dictionary. – Tim May 19 '14 at 06:00
  • It is returned by 'return str(json.dumps(resultline))'. @Tim – Nick Dong May 19 '14 at 06:02
  • Well how to convert the unicode string? `objstr.cid` got `undefined` either. @rps – Nick Dong May 19 '14 at 06:02
  • `'country': u'\u4e0a'` is causing the issue, json values ar not supposed to have anything outside `"`, but by default python strings are used as unicode, so you should not use `json.dumps()`, better use a custom encoder to output normal string instead of unicode strings. – brainless coder May 19 '14 at 06:07
  • At the beginning, it is a dictionary. And then it returned by `json.dumps()`. – Nick Dong May 19 '14 at 06:08
  • when you are assigning values to the dictionary, convert it to str, an example is like - `dict={value = str(the unicode value)}` then `json.dumps(dict)` will return actual strings instead of unicode. if this is not possible then create an encoder and output the json. – brainless coder May 19 '14 at 06:10
  • It is alreay unicode string in dictionary. It seems store as unicode in dictionary implicity in python. @Mahmud – Nick Dong May 19 '14 at 06:10
  • I got this `UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)`, when convert those unicode values. @Mahmud – Nick Dong May 19 '14 at 06:13
  • http://stackoverflow.com/questions/1207457/convert-unicode-to-a-string-in-python-containing-extra-symbols –  May 19 '14 at 06:18
  • may be this would help http://stackoverflow.com/questions/2365411/python-convert-unicode-to-ascii-without-errors – brainless coder May 19 '14 at 06:24
  • It is not requested by urllib. It is requested by ajax. So It should be not to use urllib and encode. – Nick Dong May 19 '14 at 07:30
  • The string is read from file by `readline`. I have deplay the result without `\n` as a dict using `ast`follow [link](http://stackoverflow.com/questions/13675942/converting-string-to-dict). And Transfer the result to front-end after called json.dumps(). And now JSON.parse works. – Nick Dong May 19 '14 at 11:16

0 Answers0