如何访问此JSON中的值?

时间:2021-03-24 18:37:19

I am downloading some JSON from a URL:

我从URL下载了一些JSON:

from urllib.request import Request, urlopen
import json

url = 'xyz'
r = Request(url, headers={'User-Agent':'Mozilla/5.0'})
weburl = urlopen(r)
dat = weburl.read()
encoding = weburl.info().get_content_charset('utf-8')
j = json.loads(data.decode(encoding))
print(j)

This produces something like:

这会产生类似于:

{  
   "foo":123,
   "bar":4,
   "c640103":[5,"xyz", 85.6 ...  ],
   "c63f456":[8,"pyz", 45.6 ...  ],
   "c63fdfd":[2,"xhk", 42.8 ...  ],
   "c64088a":[9,"vyi", 61.1 ...  ],
   "c63eb0c":[1,"xeq", 25.4 ...  ]
}

As can be seen, the keys after bar are a bit... odd. I have no idea to know in advance what they are or might be.

可以看出,条形后的键有点......奇怪。我不知道事先知道它们是什么或可能是什么。

How can I get foo as a variable, then get bar as a variable, and then get each of those wonky keys and their array?

我怎样才能将foo作为变量,然后将bar作为变量,然后获取每个那些不稳定的键及其数组?

I want to get something like this:

我想得到这样的东西:

foo = j['foo']
bar = j['bar']
wonky_keys = j['wonky_keys']
for i in wonky_keys:
    print(i[0]) //5
    print(i[1]) //xyz
    ...

1 个解决方案

#1


0  

You could run through the json object, when the instance of the value is list than run through the list elements, if it's not list than just print the simple value.

当值的实例是列表而不是通过列表元素运行时,您可以运行json对象,如果它不是列表而不是仅打印简单值。

js = {  
   "foo":123,
   "bar":4,
   "c640103":[5,"xyz", 85.6  ],
   "c63f456":[8,"pyz", 45.6  ],
   "c63fdfd":[2,"xhk", 42.8 ],
   "c64088a":[9,"vyi", 61.1],
   "c63eb0c":[1,"xeq", 25.4]
}

for elem in js:
  if isinstance(js[elem], list):
    for elem_li_item in js[elem]:
      print (elem_li_item)
  else:
    print (js[elem])

You can do it with js.items() as well:

您也可以使用js.items()来完成:

for k,v in js.items():
  if isinstance(v, list):
    for elem_li_item in v:
      print (elem_li_item)
  else:
    print (v)

isinstance(object, classinfo)

isinstance(object,classinfo)

Return true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof. Also return true if classinfo is a type object (new-style class) and object is an object of that type or of a (direct, indirect or virtual) subclass thereof. If object is not a class instance or an object of the given type, the function always returns false. If classinfo is a tuple of class or type objects (or recursively, other such tuples), return true if object is an instance of any of the classes or types. If classinfo is not a class, type, or tuple of classes, types, and such tuples, a TypeError exception is raised.

如果object参数是classinfo参数的实例,或者是(直接,间接或虚拟)子类的实例,则返回true。如果classinfo是类型对象(新样式类)并且object是该类型的对象或其(直接,间接或虚拟)子类,则返回true。如果object不是类实例或给定类型的对象,则该函数始终返回false。如果classinfo是类或类型对象的元组(或递归地,其他此类元组),则如果object是任何类或类型的实例,则返回true。如果classinfo不是类,类型和类型元组的类,类型或元组,则会引发TypeError异常。

#1


0  

You could run through the json object, when the instance of the value is list than run through the list elements, if it's not list than just print the simple value.

当值的实例是列表而不是通过列表元素运行时,您可以运行json对象,如果它不是列表而不是仅打印简单值。

js = {  
   "foo":123,
   "bar":4,
   "c640103":[5,"xyz", 85.6  ],
   "c63f456":[8,"pyz", 45.6  ],
   "c63fdfd":[2,"xhk", 42.8 ],
   "c64088a":[9,"vyi", 61.1],
   "c63eb0c":[1,"xeq", 25.4]
}

for elem in js:
  if isinstance(js[elem], list):
    for elem_li_item in js[elem]:
      print (elem_li_item)
  else:
    print (js[elem])

You can do it with js.items() as well:

您也可以使用js.items()来完成:

for k,v in js.items():
  if isinstance(v, list):
    for elem_li_item in v:
      print (elem_li_item)
  else:
    print (v)

isinstance(object, classinfo)

isinstance(object,classinfo)

Return true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof. Also return true if classinfo is a type object (new-style class) and object is an object of that type or of a (direct, indirect or virtual) subclass thereof. If object is not a class instance or an object of the given type, the function always returns false. If classinfo is a tuple of class or type objects (or recursively, other such tuples), return true if object is an instance of any of the classes or types. If classinfo is not a class, type, or tuple of classes, types, and such tuples, a TypeError exception is raised.

如果object参数是classinfo参数的实例,或者是(直接,间接或虚拟)子类的实例,则返回true。如果classinfo是类型对象(新样式类)并且object是该类型的对象或其(直接,间接或虚拟)子类,则返回true。如果object不是类实例或给定类型的对象,则该函数始终返回false。如果classinfo是类或类型对象的元组(或递归地,其他此类元组),则如果object是任何类或类型的实例,则返回true。如果classinfo不是类,类型和类型元组的类,类型或元组,则会引发TypeError异常。