【问题标题】:How do I turn this unicode into a dict?如何将此 unicode 转换为字典?
【发布时间】:2011-11-02 18:34:07
【问题描述】:

抱歉,我完全忽略了我正在使用 Python。让我再试一次。

我正在使用 Python 来使用返回以下 JSON 的 Web 服务:

{
  "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" : [

  ]
}

以下是我获取/解析代码中的重要 sn-ps:

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']

不幸的是,这给我留下了 results 的以下值(如 PyScripter 调试器的变量窗口中所报告的):

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'}"

例如,我不知道如何访问 addresscity 键。

你能告诉我我做错了什么,以及如何解决它吗?

谢谢, 杰米


我的问题的旧版本(已过时):

这是我正在解析的 JSON:

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'}]}

我已经深入到这个节点,它的类型是“unicode”。我该如何做一个 dict 呢?我认为它是 unicode 的事实阻止了我创建字典或访问它的键,但我不确定。

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'}"

谢谢, 杰米

【问题讨论】:

  • 这不是合法的 JSON 字符串。我怀疑您使用的语言不在标签列表中。你在开发什么,什么 DBMS?
  • 我创建了一个服务(在 ArcGIS 地理处理服务中),它返回一些看起来不错的 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":[]} 我用 headers, response = http.request(url, 'GET') 然后 responseAsJson = simplejson.loads(回应)
  • 是的。服务中的 JSON 是合法的,但原始帖子中的 JSON 不是。如果您从每个属性名称的前面删除了u,那将是合法的。
  • 是的,对不起,我的评论半响了。我还在中间... ...然后,我执行以下操作,这给出了我提到的“unicode”:results = responseAsJson['results'][0]['value' ] 你知道我做错了什么吗?
  • 请发布您的代码。什么语言? Javascript?

标签: python json unicode


【解决方案1】:

由于您的results 对象看起来像字典的字符串/unicode 版本,您需要对其进行评估。一种安全的方法(从 Python 2.6 开始)是使用 ast.literal_eval 函数:

results = ast.literal_eval(responseAsJson['results'][0]['value'])

【讨论】:

  • 是的,这行得通,谢谢! results 评估后成为 dict。
  • 你不应该这样做。您应该修复服务器中的 JSON 导出器。
  • 您好 jterrace,您会看到(在我原始帖子的第一个代码块中)服务器正在返回正确的 JSON。直到我开始用 Python 解析事物,我才最终得到 Python dict 的 unicode 表示。我仍然不确定为什么会发生这种情况,但它肯定发生在客户端而不是服务器端。
【解决方案2】:

这几乎不是一个完整的答案,因为很难说出您想要什么。但首先要纠正 JSON 字符串。

这是您可能想要的 JSON 格式:

{'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'}]}

我从几个属性名称的前面删除了u,并从名为'value'的第一个属性中删除了周围的"

现在,关于您最初的问题,我们需要更多细节。请发布您的代码以及您使用的语言/平台?从您的 cmets 中提到的responseAsJson() 猜测,您是否使用 RadAjax for ASP.NET? http://www.telerik.com/help/aspnet/ajax/ajxsimplewebserviceinvocation.html

不要在此处回复,而是将其编辑到您的帖子中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    相关资源
    最近更新 更多