[英]How could I return a json object with only specific values in python
I am new to python coding, but I currently have a Json object that has different values, ex.我是 python 编码的新手,但我目前有一个 Json object 具有不同的值,例如。
{"term_id":{"url":"http://library.austintexas.gov/taxonomy/term/205"},"name":"Ruiz Branch","address":{"latitude":"30.230228","longitude":"-97.706314"}}
and I would like to return the json object with only the adress and term_id value.我想返回 json object 只有地址和 term_id 值。
Lets say:让我们说:
a = {"term_id":{"url":"http://library.austintexas.gov/taxonomy/term/205"},"name":"Ruiz Branch","address":{"latitude":"30.230228","longitude":"-97.706314"}}
address = a['address']
print(address)
result结果
{"latitude":"30.230228","longitude":"-97.706314"}
You could delete the element you don't want like this:您可以像这样删除不需要的元素:
import json
text = '{"term_id":{"url":"http://library.austintexas.gov/taxonomy/term/205"},"name":"Ruiz Branch","address":{"latitude":"30.230228","longitude":"-97.706314"}}'
json_object = json.loads(text)
del json_object['name']
print(json_object)
{'term_id': {'url': 'http://library.austintexas.gov/taxonomy/term/205'}, 'address': {'latitude': '30.230228', 'longitude': '-97.706314'}}
If there are other possible extra elements, you could instead create a new json object to which you add only the elements you want from the original object.如果还有其他可能的额外元素,您可以改为创建一个新的 json object,您只需添加原始 object 中所需的元素即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.