简体   繁体   English

返回的python json列表的含义

[英]meaning of the returned list of python json

I'm new to python so I really don't know the language very well. 我是python的新手,所以我真的不太了解该语言。

the following example was taken from here http://docs.python.org/library/json.html 以下示例摘自此处http://docs.python.org/library/json.html

>>> import json
>>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
[u'foo', {u'bar': [u'baz', None, 1.0, 2]}]

what does the u mean? 你是什​​么意思? and how do i know which elements are available in the dictionary? 以及我怎么知道字典中有哪些元素?

It's a unicode . 这是一个unicode Iterating over the dict yields its keys: 遍历该字典将产生其密钥:

for k in D:
  print k, D[k]

Ignacio's answer a bit more verbose (no upvotes to me) 伊格纳西奥(Ignacio)的回答更加冗长(对我无投票)

u'something' means that 'something' is a unicode string, and not for instance an ascii string. u“某物”表示“某物”是unicode字符串,而不是ascii字符串。 Generally text is encoded as 8-bit characters, and you need an encoding to properly interpret/display it. 通常,文本被编码为8位字符,并且您需要进行编码才能正确解释/显示它。 Unicode is 16-bit and doesn't need seperate encodings for the various locale dependent characters. Unicode是16位的,不需要为各种与语言环境有关的字符进行单独的编码。

In a dictionary (enclosed by {}) the key is the part before the ":" and the value comes after. 在字典(由{}包围)中,键是“:”之前的部分,而值在其后。

You got a list, with elements: 您将获得一个包含以下元素的列表:

  • foo, a Unicode string foo,Unicode字符串
  • a dictionary containing: 包含以下内容的字典:
    • a key (unicode) "bar", and accessible through that key a list with values 键(unicode)“ bar”,可通过该键访问包含值的列表
      • unicode string baz, unicode字符串baz,
      • None 没有
      • a float 1.0 浮点数1.0
      • an integer 2 整数2

The python type function can be useful here. python 类型函数在这里很有用。

>>> import json
>>> data = json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
>>> data
[u'foo', {u'bar': [u'baz', None, 1.0, 2]}]
>>> type(data)
<type 'list'>
>>> type(data[0])
<type 'unicode'>
>>> type(data[1])
<type 'dict'>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM