我是Android中Api的新手。我曾使用Json对象而不是嵌套对象来进行Api响应。我对如何使用Json对象中的嵌套Json数组感到困惑。我看到了一些教程,但仍然感到困惑。
例如以下是我的Json回复:
{
"results": {
"next": "https://places.demo.api.here.com/places/v1/discover/search;context=Zmxvdy1pZD03NzgzMWY5My1hMTc3LTU1MzYtOTc0YS1lYjcyYTBmMGZjN2NfMTUzODMzOTIxMDExM184NTM0XzU0MzUmb2Zmc2V0PTIwJnNpemU9MjA?at=37.7942%2C-122.407&q=police-emergency&app_id=DemoAppId01082013GAL&app_code=AJKnXv84fjrb0KIHawS0Tg",
"items": [
{
"position": [
37.79855,
-122.40986
],
"distance": 545,
"title": "San Francisco Police Dept-Central",
"averageRating": 0,
"category": {
"id": "police-station",
"title": "Police Station",
"href": "https://places.demo.api.here.com/places/v1/categories/places/police-station?app_id=DemoAppId01082013GAL&app_code=AJKnXv84fjrb0KIHawS0Tg",
"type": "urn:nlp-types:category",
"system": "places"
},
"icon": "https://download.vcdn.cit.data.here.com/p/d/places2_stg/icons/categories/19.icon",
"vicinity": "766 Vallejo St<br/>San Francisco, CA 94133",
"having": [],
"type": "urn:nlp-types:place",
"href": "https://places.demo.api.here.com/places/v1/places/8409q8zn-a0b398e1a4664f458efe8a4cd3115bc5;context=Zmxvdy1pZD03NzgzMWY5My1hMTc3LTU1MzYtOTc0YS1lYjcyYTBmMGZjN2NfMTUzODMzOTIxMDExM184NTM0XzU0MzUmcmFuaz0w?app_id=DemoAppId01082013GAL&app_code=AJKnXv84fjrb0KIHawS0Tg",
"id": "8409q8zn-a0b398e1a4664f458efe8a4cd3115bc5",
"alternativeNames": [
{
"name": "Central District Police Station",
"language": "en"
},
{
"name": "Police Department",
"language": "en"
}
]
},
{
"position": [
37.79158,
-122.40228
],
"distance": 507,
"title": "Wang & Wang",
"averageRating": 0,
"category": {
"id": "police-station",
"title": "Police Station",
"href": "https://places.demo.api.here.com/places/v1/categories/places/police-station?app_id=DemoAppId01082013GAL&app_code=AJKnXv84fjrb0KIHawS0Tg",
"type": "urn:nlp-types:category",
"system": "places"
},
"icon": "https://download.vcdn.cit.data.here.com/p/d/places2_stg/icons/categories/19.icon",
"vicinity": "250 Montgomery St<br/>San Francisco, CA 94104",
"having": [],
"type": "urn:nlp-types:place",
"href": "https://places.demo.api.here.com/places/v1/places/8409q8yy-18f83a71fbab46c0b04fc2838ebf7867;context=Zmxvdy1pZD03NzgzMWY5My1hMTc3LTU1MzYtOTc0YS1lYjcyYTBmMGZjN2NfMTUzODMzOTIxMDExM184NTM0XzU0MzUmcmFuaz0x?app_id=DemoAppId01082013GAL&app_code=AJKnXv84fjrb0KIHawS0Tg",
"id": "8409q8yy-18f83a71fbab46c0b04fc2838ebf7867",
"openingHours": {
"text": "Mon-Fri: 08:30 - 17:00",
"label": "Opening hours",
"isOpen": false,
"structured": [
{
"start": "T083000",
"duration": "PT08H30M",
"recurrence": "FREQ:DAILY;BYDAY:MO,TU,WE,TH,FR"
}
]
}
},
{
"position": [
37.78366,
-122.4129
],
"distance": 1282,
"title": "San Francisco Police-Tenderloin",
"averageRating": 0,
"category": {
"id": "police-station",
"title": "Police Station",
"href": "https://places.demo.api.here.com/places/v1/categories/places/police-station?app_id=DemoAppId01082013GAL&app_code=AJKnXv84fjrb0KIHawS0Tg",
"type": "urn:nlp-types:category",
"system": "places"
},
"icon": "https://download.vcdn.cit.data.here.com/p/d/places2_stg/icons/categories/19.icon",
"vicinity": "301 Eddy St<br/>San Francisco, CA 94102",
"having": [],
"type": "urn:nlp-types:place",
"href": "https://places.demo.api.here.com/places/v1/places/8409q8yy-d7bbb787aa58418c917d531a5b2a51b0;context=Zmxvdy1pZD03NzgzMWY5My1hMTc3LTU1MzYtOTc0YS1lYjcyYTBmMGZjN2NfMTUzODMzOTIxMDExM184NTM0XzU0MzUmcmFuaz0xNA?app_id=DemoAppId01082013GAL&app_code=AJKnXv84fjrb0KIHawS0Tg",
"id": "8409q8yy-d7bbb787aa58418c917d531a5b2a51b0",
"alternativeNames": [
{
"name": "Police Department",
"language": "en"
},
{
"name": "SFPD - Tenderloin Station",
"language": "en"
}
]
},
{
"position": [
37.81885,
-122.26497
],
"distance": 12775,
"title": "Vigelant Pro Private Security",
"averageRating": 0,
"category": {
"id": "police-emergency",
"title": "Police/Emergency",
"href": "https://places.demo.api.here.com/places/v1/categories/places/police-emergency?app_id=DemoAppId01082013GAL&app_code=AJKnXv84fjrb0KIHawS0Tg",
"type": "urn:nlp-types:category",
"system": "places"
},
"icon": "https://download.vcdn.cit.data.here.com/p/d/places2_stg/icons/categories/19.icon",
"vicinity": "411 30th St<br/>Oakland, CA 94609",
"having": [],
"type": "urn:nlp-types:place",
"href": "https://places.demo.api.here.com/places/v1/places/8403fv6k-0c72e65d7d660fb3b53e5021d92b1283;context=Zmxvdy1pZD03NzgzMWY5My1hMTc3LTU1MzYtOTc0YS1lYjcyYTBmMGZjN2NfMTUzODMzOTIxMDExM184NTM0XzU0MzUmcmFuaz0xOQ?app_id=DemoAppId01082013GAL&app_code=AJKnXv84fjrb0KIHawS0Tg",
"id": "8403fv6k-0c72e65d7d660fb3b53e5021d92b1283"
}
]
},
"search": {
"context": {
"location": {
"position": [
37.7942,
-122.407
],
"address": {
"text": "818 Clay St<br/>San Francisco, CA 94108<br/>USA",
"house": "818",
"street": "Clay St",
"postalCode": "94108",
"district": "Chinatown",
"city": "San Francisco",
"county": "San Francisco",
"stateCode": "CA",
"country": "United States",
"countryCode": "USA"
}
},
"type": "urn:nlp-types:place",
"href": "https://places.demo.api.here.com/places/v1/places/loc-dmVyc2lvbj0xO3RpdGxlPTgxOCtDbGF5K1N0O2xhdD0zNy43OTQyO2xvbj0tMTIyLjQwNztzdHJlZXQ9Q2xheStTdDtob3VzZT04MTg7Y2l0eT1TYW4rRnJhbmNpc2NvO3Bvc3RhbENvZGU9OTQxMDg7Y291bnRyeT1VU0E7ZGlzdHJpY3Q9Q2hpbmF0b3duO3N0YXRlQ29kZT1DQTtjb3VudHk9U2FuK0ZyYW5jaXNjbztjYXRlZ29yeUlkPWJ1aWxkaW5nO3NvdXJjZVN5c3RlbT1pbnRlcm5hbA;context=c2VhcmNoQ29udGV4dD0x?app_id=DemoAppId01082013GAL&app_code=AJKnXv84fjrb0KIHawS0Tg"
},
"supportsPanning": true,
"ranking": "category-recommendations"
}
}
我知道外面的大括号表示对象,而其他方括号是json数组,但是我对如何解析它感到困惑。我使用Retrofit lib tho
我尝试使用POJO,但是在调试应用程序时,断点在创建ApiInterface对象时中断,没有任何消息。早些时候我的身体为null,没有任何错误(我已经硬编码了要在@PATH中传递的值)。
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
Call<Results> call = apiService.getALlData();
call.enqueue(new Callback<Results>() {
@Override
public void onResponse(Call<Results> call, Response<Results> response) {
ArrayList<Item> items = response.body().getItems();
rc.setAdapter(new DataAdapter(getApplicationContext(), items));
}
@Override
public void onFailure(Call<Results> call, Throwable t) {
}
});
我的网址:https://places.cit.api.here.com/places/v1/discover/search?at= {LAT / LONG}&q = police-emergency&Accept-Language = zh-CN%2Cen%3Bq%3D0.9&app_id = {APP_ID}&app_code = {AP_CODE}
我导致的实施无法正常工作。
@GET("search?at={latlong}&q=police-emergency&Accept-Language=en-US%2Cen%3Bq%3D0.9&app_id=********&app_code=**********")
Call<Results> getAllData(@Path("") String s);
任何概念或帮助将不胜感激。 预先感谢。
答案 0 :(得分:0)
Json数组被解析为ArrayList,对于您的代码,您需要这样的东西:
public class QueryResult {
private Results results;
private Search search;
/* getters and setters */
}
class Results{
private String next;
private ArrayList<Item> items;
/* getters and setters */
}
class Item{
private ArrayList<Float> position;
private int distance;
private String title;
private int averageRating;
private Category category;
(...)
}
然后,您需要创建带有其字段等的Category对象。
最后,您可以拨打改造电话:
call.enqueue(new Callback<QueryResult>() {
@Override
public void onResponse(Call<QueryResult> call, Response<QueryResult> response) {
QueryResult result = response.body()
}
@Override
public void onFailure(Call<QueryResult> call, Throwable t) {
}
});