Reputation: 1305
Sorry, I completely neglected to mention that I'm using Python. Let me try this again.
I'm using Python to consume a web service that returns the following JSON:
{
"results" : [
{
"paramName" : "output",
"dataType" : "GPString",
"value" : "{'city': 'Falls Church', 'isRuralArea': False, 'zip': '22046', 'isNotInBTOPLMArea': True, 'longitude': '-77.186180', 'isGeocodable': True, 'county': u'Falls Church', 'isNotInBIPLMArea': True, 'state': 'VA', 'isLatLongInUSBounds': True, 'address': '604 Jackson St', 'latitude': '38.884937'}"
}
],
"messages" : [
]
}
Here are the important snippets from my fetching/parsing code:
import urllib
import httplib2
import json
import simplejson
http = httplib2.Http()
headers, response = http.request(url, 'GET')
if headers['status'] == "200":
responseAsJson = simplejson.loads(response)
print "response = " + repr(responseAsJson)
results = responseAsJson['results'][0]['value']
Unfortunately, this leaves me with the following value for results (as reported in PyScripter Debugger's Variables window):
u"{'city': 'Falls Church', 'isRuralArea': False, 'zip': '22046', 'isNotInBTOPLMArea': True, 'longitude': '-77.186180', 'isGeocodable': True, 'county': u'Falls Church', 'isNotInBIPLMArea': True, 'state': 'VA', 'isLatLongInUSBounds': True, 'address': '604 Jackson St', 'latitude': '38.884937'}"
I don't know how to then access the address or city keys, for instance.
Can you tell what I'm doing wrong, and how to fix it?
Thanks, Jamie
Old version of my question (obsolete):
Here's the JSON I'm parsing:
response = {u'messages': [], u'results': [{u'dataType': u'GPString', u'value': u"{'city': 'Falls Church', 'isRuralArea': False, 'zip': '22046', 'isNotInBTOPLMArea': True, 'longitude': '-77.186180', 'isGeocodable': True, 'county': u'Falls Church', 'isNotInBIPLMArea': True, 'state': 'VA', 'isLatLongInUSBounds': True, 'address': '604 Jackson St', 'latitude': '38.884937'}", u'paramName': u'output'}]}
I've drilled down to this node, and its type is "unicode." How do I make a dict out of this? I think the fact that it's unicode is preventing me from creating the dict or accessing its keys, but I'm not sure.
u"{'city': 'Falls Church', 'isRuralArea': False, 'zip': '22046', 'isNotInBTOPLMArea': True, 'longitude': '-77.186180', 'isGeocodable': True, 'county': u'Falls Church', 'isNotInBIPLMArea': True, 'state': 'VA', 'isLatLongInUSBounds': True, 'address': '604 Jackson St', 'latitude': '38.884937'}"
Thanks, Jamie
Upvotes: 3
Views: 4529
Reputation: 2543
Since your results
object looks like a string/unicode version of a dict, you need to eval it. A safe way of doing this (as of Python 2.6) would be to use the ast.literal_eval function:
results = ast.literal_eval(responseAsJson['results'][0]['value'])
Upvotes: 2
Reputation: 17451
This is not nearly a complete answer, since it's hard to tell what you're wanting. But a place to start is correcting the JSON string.
Here's the format of JSON you're probably wanting:
{'messages': [], 'results': [{'dataType': 'GPString', 'value': {'city': 'Falls Church', 'isRuralArea': False, 'zip': '22046', 'isNotInBTOPLMArea': True, 'longitude': '-77.186180', 'isGeocodable': True, 'county': 'Falls Church', 'isNotInBIPLMArea': True, 'state': 'VA', 'isLatLongInUSBounds': True, 'address': '604 Jackson St', 'latitude': '38.884937'}, 'paramName': 'output'}]}
I removed the u
from the front of several attribute names and removed the surrounding "
from the first attribute named 'value'
.
Now, in regard to your original question, we need more detail. Please post your code and what language/platform you're using? Guessing from the responseAsJson()
mentioned in your comments, are you using RadAjax for ASP.NET? http://www.telerik.com/help/aspnet/ajax/ajxsimplewebserviceinvocation.html
Don't respond here, but instead edit it into your post.
Upvotes: 0