简体   繁体   English

我怎么能返回一个 json object 只有特定值在 python

[英]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.

相关问题 如何仅返回 API JSON 响应中的一组值中的特定值? - How do I only return a specific value in a set of values in an API JSON response? 解析python中的Json文件返回具体值 - Parsing Json file in python to return specific values 从 Json 返回特定路径 python 的值 - Return values from a Json for a specific path python 使用 Python 脚本仅返回特定值 - Return Only Specific Values With Python Script 如何使用Flask在Python中使用JSON返回单个对象 - How do I return single object with JSON in Python with Flask 如何从所有 mongodb 文档中仅获取特定值并将它们作为 json 返回? - How to get only specific values from all mongodb documents and return them as json? 使用 Python Django 我怎么能做自定义 function,保存到数据库并返回 Z466DEEC716ECDF5FCA54D8 响应 - Using Python Django how could I do custom function, save to db and return json response Python / IronPython-当仅引用类对象时,如何返回类的特定属性的值? - Python/IronPython- how to return value of specific attribute of class when only class object referenced? 如何返回特定的json字段? - How do I return a specific json field? 如何使模拟对象的任意嵌套属性/方法返回 Python 中的特定值? - How to make a mocked object's arbitrary nested attributes/methods to return specific values in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM