I'm doing a school project (newbie alert) where I need to use the zomato's website to get a cuisine's ID and name depending on the ID I send. This is the code in the demo file:
我正在做一个学校项目(新手警报),我需要使用zomato的网站获取菜肴的ID和名称,具体取决于我发送的ID。这是演示文件中的代码:
WebServiceConnection web = new WebServiceConnection("xxxxxxxxxxxx");
AcessoDados acesso = new AcessoDados("https://api.zomato.com/v1/cuisines.json", web);
String aces = acesso.getCuisines(310);
JSONObject obj = new JSONObject();
System.out.println(obj.get(aces));
All the methods called here were made by our teacher.
这里所说的所有方法都是我们的老师做的。
WebServiceConnection and AcessoDados function get the link to Zomato's API and the user key. (Zomato API requires a key to work)
WebServiceConnection和AcessoDados函数获取Zomato的API和用户密钥的链接。 (Zomato API需要一键才能工作)
"acesso.getCuisines(310)" sends the the value "310", and the method "getCuisines" should return a json string with all the establishments it can find in their website (310 is the ID for Porto, Portugal).
“acesso.getCuisines(310)”发送值“310”,方法“getCuisines”应该返回一个json字符串,其中包含它可以在其网站中找到的所有机构(310是葡萄牙波尔图的ID)。
However, it only prints "[]" in the output (without the quotes). It should print something like this:
但是,它只在输出中打印“[]”(不带引号)。它应该打印这样的东西:
[
{"cuisine_name":"African","cuisine_id":152},
{"cuisine_name":"American","cuisine_id":1},
{"cuisine_name":"Angolan","cuisine_id":951},
{"cuisine_name":"Cafe","cuisine_id":30},
(...)
]
I can't find what the problem is or if I'm making any obvious mistake. Am I missing something here?
我找不到问题是什么,或者我是否犯了任何明显的错误。我在这里错过了什么吗?
1 个解决方案
#1
1
If System.out.println(aces);
prints []
, then Zomato's API is returning an empty response, which means that 310
is not a valid id
. But, if aces
prints a non-empty string and is a JSON String then the following solution should work.
如果是System.out.println(aces);打印[],然后Zomato的API返回一个空响应,这意味着310不是有效的ID。但是,如果aces打印非空字符串并且是JSON字符串,则以下解决方案应该有效。
You are not parsing the JSON string which was received by Zomato API. Instead, you are creating a new JSONObject
which is empty.
您没有解析Zomato API收到的JSON字符串。相反,您正在创建一个空的新JSONObject。
Try doing the following :
尝试执行以下操作:
JSONObject obj = null;
try {
obj = (JSONArray) JSONValue.parseWithException(aces);
System.out.println(obj);
} catch (ParseException e) {
e.printStackTrace();
}
Assumptions :
- You are using json-simple
你正在使用json-simple
#1
1
If System.out.println(aces);
prints []
, then Zomato's API is returning an empty response, which means that 310
is not a valid id
. But, if aces
prints a non-empty string and is a JSON String then the following solution should work.
如果是System.out.println(aces);打印[],然后Zomato的API返回一个空响应,这意味着310不是有效的ID。但是,如果aces打印非空字符串并且是JSON字符串,则以下解决方案应该有效。
You are not parsing the JSON string which was received by Zomato API. Instead, you are creating a new JSONObject
which is empty.
您没有解析Zomato API收到的JSON字符串。相反,您正在创建一个空的新JSONObject。
Try doing the following :
尝试执行以下操作:
JSONObject obj = null;
try {
obj = (JSONArray) JSONValue.parseWithException(aces);
System.out.println(obj);
} catch (ParseException e) {
e.printStackTrace();
}
Assumptions :
- You are using json-simple
你正在使用json-simple