[英]"Failed to convert value of type 'java.lang.String' to required 'java.util.List'." Or how to convert Python list to Java.util list?
This one has the better of me, for sure.这个肯定比我好。
I am attempting to use the USDA FoodData Central Rest API for Python, and I have extensively reviewed the related documentation, online materials and SwaggerHub simulations.我正在尝试将USDA FoodData Central Rest API 用于 Python,并且我已广泛查看相关文档、在线材料和 SwaggerHub 模拟。
To make a long story short, it seems I need to convert a Python list to a 'java.util.List', something not in the USDA documentation , and something I have not been able to make happen.长话短说,似乎我需要将 Python 列表转换为“java.util.List”,这在USDA 文档中没有,而且我无法实现。 Despite my efforts, I am unable to even attempt to make that conversion happen, because I cannot install the Python dependencies .
尽管我付出了努力,但我什至无法尝试进行这种转换,因为我无法安装 Python 依赖项。 It boils down to two questions overall:
总体上归结为两个问题:
The USDA site sends you to a SwaggerHub simulation.美国农业部网站会将您带到 SwaggerHub 模拟。 It was helpful, and it gave me the URL pasted below as a model for my API. However, the instructions regarding the required 'fdcIds list' says nothing at all about the need to convert to Java.
这很有帮助,它给了我下面粘贴的 URL 作为我的 API 的 model。但是,关于所需“fdcIds 列表”的说明根本没有说明需要转换为 Java。
To be clear, the 'fdcIds list' is simply the desired Python list of the six-digit numbers that represent all the food items and products in the USDA database.需要明确的是,“fdcIds 列表”只是所需的 Python 六位数字列表,代表美国农业部数据库中的所有食品和产品。 You can request up to 20 at a time and the API returns a complete nutritional profile of each.
您一次最多可以请求 20 个,API 会返回每个的完整营养概况。 You will see what I mean if you follow the SwaggerHub link below
如果您点击下面的 SwaggerHub 链接,您就会明白我的意思
Here is the Swaggerhub model URL with the link below:这是 Swaggerhub model URL,链接如下:
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
Here are SwaggerHub instructions for the fdcID list pasted in their entirety:以下是完整粘贴的 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)."
I think the rest of my code (below) is fine, because otherwise the SwaggerHub simulation produces the desired results.我认为我的代码(下方)的 rest 很好,否则 SwaggerHub 模拟会产生所需的结果。
Here is the complete error message I received afterwards:这是我之后收到的完整错误消息:
'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'}
I know Python and JavaScript. I know nothing about Java.我知道Python和JavaScript。我对Java一无所知。
However, I attempted to install the Python packages necessary for this operation, such as jdk and jep , but I ran into additional error messages, as it seems the packages are deprecated(?) - though I am not sure.但是,我尝试安装此操作所需的 Python 包,例如jdk和jep ,但我遇到了其他错误消息,因为这些包似乎已被弃用(?) - 虽然我不确定。 I forced the jep installation, but it still did not install successfully.
我强行安装了jep,还是没有安装成功。
The error messages related to the failed installations are too long to paste here;与失败安装相关的错误消息太长,无法粘贴到此处; but I will provide examples that seem to represent the gist of the problem.
但我将提供似乎代表问题要点的示例。
error: subprocess-exited-with-error
error: metadata-generation-failed
note: This error originates from a subprocess, and is likely not a problem with pip.
I have spent the past several trying to make this API work.在过去的几年里,我一直在努力使这个 API 工作。 I've been working on the related app for almost a year.
我已经在相关应用程序上工作了将近一年。 I will send a request for technical assistance to the USDA, but I could use some help if anyone has some genuine insights or has resolved similar issues.
我会向美国农业部发送技术援助请求,但如果有人有一些真正的见解或解决了类似问题,我可以提供一些帮助。
The USDA API is a popular one.美国农业部 API 是一个受欢迎的。 It apparently works for most developers.
它显然适用于大多数开发人员。 I am hoping someone can help.
我希望有人能提供帮助。 I am a bit mystified by these obstacles.
我对这些障碍有点迷惑。
My operating system is Windows 10 64 bit.我的操作系统是 Windows 10 64 位。
My code, as it stands, is pasted below.我的代码,就目前而言,粘贴在下面。
I should note the variables in the URL were originally in a "parameters" dictionary.我应该注意到 URL 中的变量最初在“参数”字典中。 But then, I started to receive error messages stating the dictionary did not contain the fields in the requested URL, which was not true.
但随后,我开始收到错误消息,指出字典不包含请求的 URL 中的字段,这是不正确的。 It seemed to work better when I made them standalone variables...except for the conversion issues related to the fdcIds list.
当我将它们设为独立变量时,它似乎工作得更好......除了与 fdcIds 列表相关的转换问题。
At that point, I started to receive error messages like the one in the title related to my failure to convert the Python list to a Java Util.List.在那一点上,我开始收到错误消息,如标题中与我未能将 Python 列表转换为 Java Util.List 相关的错误消息。 I have reviewed all the online information and videos I could find about Python rest apis and possible solutions to the Java conversion problem, including all related questions on Stackoverflow.
我已经查看了所有我能找到的关于 Python rest api 和 Java 转换问题的可能解决方案的在线信息和视频,包括 Stackoverflow 上的所有相关问题。
I am at a loss as to how I should proceed.我不知道该如何进行。
Your sincere feedback is much appreciated.非常感谢您的真诚反馈。
Thank you for reviewing my question.感谢您查看我的问题。
Below, I pasted my code in its entirety.下面,我完整地粘贴了我的代码。 Again, I have also tried to place the variables in the URL inside a parameters dictionary.
同样,我还尝试将 URL 中的变量放在参数字典中。 But it only caused more trouble.
但这只会带来更多麻烦。
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)
Change fdcIds
as shown below and also foodNutrients
:如下所示更改
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)
Result:结果:
[{'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'}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.