Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #53391

Re: How do I process this using Python? (SOLVED)

Date 2013-08-31 19:58 -0500
From Anthony Papillion <papillion@gmail.com>
Subject Re: How do I process this using Python? (SOLVED)
References <52227FC4.1060208@gmail.com> <CAPTjJmoHeDrUwpuiwx9+Ejz7oE7dOyYibjoTXWu4ycr7fZ2exg@mail.gmail.com> <5222881C.8060205@gmail.com> <CAPTjJmr6fZxF+4s-QDNa8C2HLwhDgEd5HeoUwekvYzyJVX+9yw@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.435.1377997133.19984.python-list@python.org> (permalink)

Show all headers | View raw


On 08/31/2013 07:32 PM, Chris Angelico wrote:
> On Sun, Sep 1, 2013 at 10:19 AM, Anthony Papillion <papillion@gmail.com> wrote:
>> On 08/31/2013 06:48 PM, Chris Angelico wrote:
>>> On Sun, Sep 1, 2013 at 9:44 AM, Anthony Papillion <papillion@gmail.com> wrote:
>>>> I'm writing a processor for Bitmessage messages and I am needing to
>>>> parse the following returned JSON string:
>>>>
>>>> {u'inboxMessages':
>>>
>>> Does the JSON string really have those u prefixes and apostrophes?
>>> That's not valid JSON. You may be able to use ast.literal_eval() on it
>>> - I was able to with the example data - but not a JSON parser. Can you
>>> sort out your transmission end?
>>>
>>> ChrisA
>>
>> I think I remembered what the 'u' prefix is. It indicates that the data
>> following is a unicode string. So could that be valid JSON data wrapped
>> up in unicode?
> 
> No; JSON already supports Unicode. What you may have is an incorrect
> JSON encoder that uses Python's repr() to shortcut its work - but it's
> wrong JSON.
> 
> But bitmessage seems to be written in Python. Can you simply access
> the objects it's giving you, rather than going via text strings?
> 
> ChrisA

Once I looked at the data in a better structured way and saw it in the
proper type it became clear. Here is my solution:

data = json.loads(api.getAllInboxMessages())
for message in data['inboxmessages']:
    print message['fromAddress']

Yep, it was that simple. Thanks for the help guys! I appreciate how fast
everyone responded.

Anthony

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: How do I process this using Python? (SOLVED) Anthony Papillion <papillion@gmail.com> - 2013-08-31 19:58 -0500

csiph-web