"Failed to convert value of type 'java.lang.String' to required 'java.util.List'." 或者如何将 Python 列表转换为 Java.util 列表?

"Failed to convert value of type 'java.lang.String' to required 'java.util.List'." Or how to convert Python list to Java.util list?

这个肯定比我好。

我正在尝试将 USDA FoodData Central Rest API 用于 Python,并且我已广泛查阅相关文档、在线资料和 SwaggerHub 模拟。

长话短说,我似乎需要将 Python 列表转换为 'java.util.List',the USDA documentation 中没有的内容,以及我需要的内容未能实现。尽管我付出了努力,但我什至无法 尝试 进行转换,因为我无法安装 Python 依赖项 。 总的来说可以归结为两个问题:

  1. 如果可能,如何将 Python 列表转换为 Java.Util.List?
  2. 当安装依赖项(例如 jep)似乎已被弃用时出现问题,我该怎么办?

美国农业部网站将您发送到 SwaggerHub 模拟。它很有帮助,它给了我下面粘贴的 URL 作为我的 API 的模型。但是,关于 required 'fdcIds list' 的说明根本没有说明需要转换为 Java.

明确地说,'fdcIds list' 只是代表美国农业部数据库中所有食品和产品的 six-digit 数字的所需 Python 列表.您一次最多可以请求 20 个,并且 API returns 每个的完整营养概况。 如果您关注下面的 SwaggerHub link

,您就会明白我的意思

这是 Swaggerhub 模型 URL,下面 link:

https://api.nal.usda.gov/fdc/v1/foods?fdcIds=173567&fdcIds=173565&fdcIds=173571&format=abridged&nutrients=208&nutrients=204&nutrients=205&api_key=xxxxxxxxxxxxxxxx

https://app.swaggerhub.com/apis/fdcnal/food-data_central_api/1.0.0#/info 

以下是完整粘贴的 fdcID 列表的 SwaggerHub 说明:

"fdcIds (reguired)
array[string]
(query)
List of multiple FDC ID's. Should be comma separated list (e.g. fdcIds=534358,373052) or repeating parameters (e.g. fdcIds=534358&fdcIds=373052)."

我认为我的其余代码(下方)没有问题,否则 SwaggerHub 模拟会产生所需的结果。

这是我之后收到的完整错误信息:


'error': 'Bad Request',
 'message': "Failed to convert value of type 'java.lang.String' to required "
            "type 'java.util.List'; nested exception is "
            'java.lang.NumberFormatException: For input string: '
            '"[{\'fdcids\':173567},{\'fdcids\':173565},{\'fdcids\':173571},{\'fdcids\':173569},{\'fdcids\':173564}]"',
 'path': '/portal-data/api/v1/foods',
 'status': 400,
 'timestamp': '2022-04-15T21:49:49.609+0000'}

我知道 Python 和 Java 脚本。我对 Java.

一无所知

但是,我尝试安装此操作所需的 Python 包,例如 jdk and jep,但我 运行 进入其他错误消息,因为这些包似乎已被弃用(?) - 虽然我不确定。我强制安装了jep,还是没有安装成功

与安装失败相关的错误消息太长,无法粘贴到此处;但我将提供似乎代表问题要点的示例。

error: subprocess-exited-with-error
error: metadata-generation-failed
note: This error originates from a subprocess, and is likely not a problem with pip.

过去几年我一直在努力使这个 API 工作。我已经在相关应用程序上工作了将近一年。我会向美国农业部发送技术援助请求,但如果有人有真正的见解或解决了类似问题,我可以提供一些帮助。

美国农业部 API 很受欢迎。它显然适用于大多数开发人员。我希望有人能提供帮助。我对这些障碍有点迷惑。

我的操作系统是 Windows 10 64 位。

下面粘贴了我的代码。

我应该注意到 URL 中的变量最初在“参数”字典中。 但随后,我开始收到错误消息,指出字典不包含请求的 URL 中的字段,这是不正确的。当我将它们设为独立变量时,它似乎工作得更好......除了与 fdcIds 列表相关的转换问题。

那时,我开始收到标题中的错误消息,是关于我未能将 Python 列表转换为 Java Util.List。我查看了所有我能找到的关于 Python rest api 和 Java 转换问题的可能解决方案的在线信息和视频,包括 Whosebug 上的所有相关问题。

我不知道该如何继续。

非常感谢您的真诚反馈。

感谢您审阅我的问题。

下面,我完整地粘贴了我的代码。同样,我还尝试将变量放在参数字典内的 URL 中。但这只会带来更多的麻烦。

import requests
import pprint

api_key = 'AcAm9H2jcFikw4f5PmJl0hFJSjreMwg9BN621tQD'               
pagesize = 5,
fdcIds = [{'fdcids': 173567}, {'fdcids': 173565}, {'fdcids': 173571}, {'fdcids': 173569}, {'fdcids': 173564}]
format = 'abridged'
dataType = 'SR Legacy'
foodNutrients = [208, 204,205]

url = f"https://api.nal.usda.gov/fdc/v1/foods?fdcIds={fdcIds}&format={format}&dataType={dataType}&nutrients={foodNutrients}&pagesize={pagesize}'&api_key={api_key}"

headers = {'Content-Type': 'application/json;charset=utf-8'}

data = requests.get(url, headers).json()
print(requests.status_codes)
pprint.pprint(data)

您可以通过两种方式传递 fdcId:

Should be comma separated list (e.g. fdcIds=534358,373052) or repeating parameters (e.g. fdcIds=534358&fdcIds=373052).

这里提到here。尝试以这些格式传递 ID。

如下所示更改 fdcIds 以及 foodNutrients:

import requests
import pprint

api_key = 'AcAm9H2jcFikw4f5PmJl0hFJSjreMwg9BN621tQD'          
pagesize = 5
# fdcIds = [{'fdcids': 173567}, {'fdcids': 173565}, {'fdcids': 173571}, {'fdcids': 173569}, {'fdcids': 173564}]
fdcIds = "173567,173565,173571,173569,173564"
f = 'abridged'
dataType = 'SR Legacy'
foodNutrients = "208, 204, 205"

url = f"https://api.nal.usda.gov/fdc/v1/foods?fdcIds={fdcIds}&format={f}&dataType={dataType}&nutrients={foodNutrients}&pagesize={pagesize}'&api_key={api_key}"
print(url)
headers = {'Content-Type': 'application/json;charset=utf-8'}

data = requests.get(url, headers).json()
print(requests.status_codes)
pprint.pprint(data)

结果:

[{'dataType': 'SR Legacy',
  'description': 'Shortening frying (heavy duty), beef tallow and cottonseed',
  'fdcId': 173567,
  'foodNutrients': [{'amount': 100,
                     'name': 'Total lipid (fat)',
                     'number': '204',
                     'unitName': 'G'},
                    {'amount': 0.0,
                     'name': 'Carbohydrate, by difference',
                     'number': '205',
                     'unitName': 'G'},
                    {'amount': 900,
                     'derivationCode': 'NC',
                     'derivationDescription': 'Calculated',
                     'name': 'Energy',
                     'number': '208',
                     'unitName': 'KCAL'}],
  'ndbNumber': '4550',
  'publicationDate': '2019-04-01'},
 {'dataType': 'SR Legacy',
  'description': 'Fat, chicken',
  'fdcId': 173564,
  'foodNutrients': [{'amount': 900,
                     'derivationCode': 'NC',
                     'derivationDescription': 'Calculated',
                     'name': 'Energy',
                     'number': '208',
                     'unitName': 'KCAL'},
                    {'amount': 99.8,
                     'name': 'Total lipid (fat)',
                     'number': '204',
                     'unitName': 'G'},
                    {'amount': 0.0,
                     'name': 'Carbohydrate, by difference',
                     'number': '205',
                     'unitName': 'G'}],
  'ndbNumber': '4542',
  'publicationDate': '2019-04-01'},
 {'dataType': 'SR Legacy',
  'description': 'Fat, turkey',
  'fdcId': 173571,
  'foodNutrients': [{'amount': 99.8,
                     'name': 'Total lipid (fat)',
                     'number': '204',
                     'unitName': 'G'},
                    {'amount': 0.0,
                     'name': 'Carbohydrate, by difference',
                     'number': '205',
                     'unitName': 'G'},
                    {'amount': 900,
                     'derivationCode': 'NC',
                     'derivationDescription': 'Calculated',
                     'name': 'Energy',
                     'number': '208',
                     'unitName': 'KCAL'}],
  'ndbNumber': '4575',
  'publicationDate': '2019-04-01'},
 {'dataType': 'SR Legacy',
  'description': 'Oil, soybean, salad or cooking, (partially hydrogenated) and '
                 'cottonseed',
  'fdcId': 173565,
  'foodNutrients': [{'amount': 100,
                     'name': 'Total lipid (fat)',
                     'number': '204',
                     'unitName': 'G'},
                    {'amount': 0.0,
                     'name': 'Carbohydrate, by difference',
                     'number': '205',
                     'unitName': 'G'},
                    {'amount': 884,
                     'derivationCode': 'NC',
                     'derivationDescription': 'Calculated',
                     'name': 'Energy',
                     'number': '208',
                     'unitName': 'KCAL'}],
  'ndbNumber': '4543',
  'publicationDate': '2019-04-01'},
 {'dataType': 'SR Legacy',
  'description': 'Shortening industrial, soybean (hydrogenated) and cottonseed',
  'fdcId': 173569,
  'foodNutrients': [{'amount': 0.0,
                     'name': 'Carbohydrate, by difference',
                     'number': '205',
                     'unitName': 'G'},
                    {'amount': 884,
                     'derivationCode': 'NC',
                     'derivationDescription': 'Calculated',
                     'name': 'Energy',
                     'number': '208',
                     'unitName': 'KCAL'},
                    {'amount': 100,
                     'derivationCode': 'A',
                     'derivationDescription': 'Analytical',
                     'name': 'Total lipid (fat)',
                     'number': '204',
                     'unitName': 'G'}],
  'ndbNumber': '4554',
  'publicationDate': '2019-04-01'}]