简体   繁体   English

如何仅返回对象包含来自 XML/JSON URL 的字符串

[英]How return only object contain string from XML/JSON URL

If there anyone can help me get only string i need from parsing URL:如果有人可以帮助我从解析 URL 中只获取我需要的字符串:

import requests 
import json

def get_stock(sku):
    params = {'ItemId': sku}
    base_url = 'http://10.0.0.25/api/GetSku?ItemId='
    response = requests.get(base_url, params=params)
    json_parsed = json.loads(response.read)
     
    return json_parsed



    
print(get_stock(101025HRLONDON))

The output is:输出是:

[
   {
        "ItemId": "101025HRLONDON",
        "Site": "12",
        "Warehouse": "17",
        "availablePhysical": 1.0
    },
    {
        "ItemId": "101025HRLONDON",
        "Site": "33",
        "Warehouse": "33",
        "availablePhysical": 1.0
    },
    {
        "ItemId": "101025HRLONDON",
        "Site": "12",
        "Warehouse": "34",
        "availablePhysical": 1.0
    },
    {
        "ItemId": "101025HRLONDON",
        "Site": "77",
        "Warehouse": "42",
        "availablePhysical": 1.0
    }
]

The code works well and returns all products stock by ItemID.该代码运行良好,并按 ItemID 返回所有产品库存。

But my question is how I can return an only object that contains : "Site":"12"但我的问题是如何返回包含以下内容的唯一对象: "Site":"12"

Thank you!谢谢!

Replace:代替:

return json_parsed

By:经过:

if any(i['Site'] == '12' for i in json_parsed):
    return json_parsed

You can filter the list using the key you want.您可以使用所需的键filter列表。 The function you've written is correct, you only need to manipulate the returned data.你写的函数是正确的,你只需要操作返回的数据。

filtered_stocks = list(filter(lambda x: x['Site']=="12", get_stock("101025HRLONDON"))
print(filtered_stocks)

something like the below类似下面的东西

import requests 
from typing import List,Dict


def get_stock(sku:str,site_id:str)-> List[Dict]:
    base_url = 'http://10.0.0.25/api/GetSku?ItemId='
    response = requests.get(base_url, params={'ItemId': sku})
    if response.status_code == 200:
      return [item for item in response.json() if item['Site'] == site_id]
    else:
      return None

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

相关问题 如何打印仅包含字符串中字母的单词? - how to print words that only contain letters from a string? 如何从Flask返回JSON对象 - How to return JSON object from flask 如何从字符串中仅获取 URL - How to get only URL from a string 我怎么能返回一个 json object 只有特定值在 python - How could I return a json object with only specific values in python XML转换无法返回JSON返回对象 - XML conversion fails to JSON return object 如何使用 python 从仅包含斜线的 url 中提取参数? - How do you extract parameters from a url that only contain slash using python? 我们如何从字符串中获取对象引用? 这样的字符串必须包含哪些信息? - How can we get an object reference from a string? What information must such a string contain? 仅保留包含字符串列表中的字符串的 df 列值 - Only keep df column values that contain a string from list of string 将url解析为json后,无法从列表(仅整个列表)返回特定元素 - After parsing url as json, can't return a specific element from list- only whole list Python-如何确保从文件读取的行仅包含给定的字符串,而没有其他内容 - Python - How to make sure that a line being read from a file contain only a given string and nothing else
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM