0

我在家里使用 Java 和 Akka 设计新应用程序,但是我在传递 Zomato 的 api 响应时遇到了问题。

JSON 响应可以在下面找到。我用对象列表创建了 Class Restaurant 餐厅。我应该如何解析它?

{
"results_found": 131,
"results_start": 0,
"results_shown": 10,
"restaurants": [
    {
        "restaurant": {
            "R": {
                "res_id": 6103211
            },
            "apikey": "4972ea7a10293fc07e997364eef03d3d",
            "id": "6103211",
            "name": "Dishoom",
            "url": "https://www.zomato.com/london/dishoom-covent-garden?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1",
            "location": {
                "address": "12 Upper St Martin's Lane, Covent Garden, London WC2H 9FB",
                "locality": "Upper St Martin's Lane, Covent Garden",
                "city": "London",
                "city_id": 61,
                "latitude": "51.5124170000",
                "longitude": "-0.1271640000",
                "zipcode": "WC2H 9FB",
                "country_id": 215,
                "locality_verbose": "Upper St Martin's Lane, Covent Garden, London"
            },
            "switch_to_order_menu": 0,
            "cuisines": "Indian, North Indian, Curry, Cafe",
            "average_cost_for_two": 35,
            "price_range": 2,
            "currency": "\u00a3",
            "offers": [],
            "thumb": "https://b.zmtcdn.com/data/res_imagery/6103211_RESTAURANT_19f7b41684765da1a700c2818f16cde0.jpg?fit=around%7C200%3A200&crop=200%3A200%3B%2A%2C%2A",
            "user_rating": {
                "aggregate_rating": "4.9",
                "rating_text": "Excellent",
                "rating_color": "3F7E00",
                "votes": "919"
            },
            "photos_url": "https://www.zomato.com/london/dishoom-covent-garden/photos?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1#tabtop",
            "menu_url": "https://www.zomato.com/london/dishoom-covent-garden/menu?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1&openSwipeBox=menu&showMinimal=1#tabtop",
            "featured_image": "https://b.zmtcdn.com/data/res_imagery/6103211_RESTAURANT_19f7b41684765da1a700c2818f16cde0.jpg",
            "has_online_delivery": 0,
            "is_delivering_now": 0,
            "include_bogo_offers": true,
            "deeplink": "zomato://restaurant/6103211",
            "has_table_booking": 0,
            "events_url": "https://www.zomato.com/london/dishoom-covent-garden/events#tabtop?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1",
            "establishment_types": []
        }
    },
4

2 回答 2

0

如果 json 与您在此处粘贴的内容完全相同,则它似乎不是有效的 json。试试这个:

  {
    "results_found": 131,
    "results_start": 0,
    "results_shown": 10,
    "restaurants": [{
        "restaurant": {
            "R": {
                "res_id": 6103211
            },
            "apikey": "4972ea7a10293fc07e997364eef03d3d",
            "id": "6103211",
            "name": "Dishoom",
            "url": "https://www.zomato.com/london/dishoom-covent-garden?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1",
            "location": {
                "address": "12 Upper St Martin's Lane, Covent Garden, London WC2H 9FB",
                "locality": "Upper St Martin's Lane, Covent Garden",
                "city": "London",
                "city_id": 61,
                "latitude": "51.5124170000",
                "longitude": "-0.1271640000",
                "zipcode": "WC2H 9FB",
                "country_id": 215,
                "locality_verbose": "Upper St Martin's Lane, Covent Garden, London"
            },
            "switch_to_order_menu": 0,
            "cuisines": "Indian, North Indian, Curry, Cafe",
            "average_cost_for_two": 35,
            "price_range": 2,
            "currency": "\u00a3",
            "offers": [],
            "thumb": "https://b.zmtcdn.com/data/res_imagery/6103211_RESTAURANT_19f7b41684765da1a700c2818f16cde0.jpg?fit=around%7C200%3A200&crop=200%3A200%3B%2A%2C%2A",
            "user_rating": {
                "aggregate_rating": "4.9",
                "rating_text": "Excellent",
                "rating_color": "3F7E00",
                "votes": "919"
            },
            "photos_url": "https://www.zomato.com/london/dishoom-covent-garden/photos?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1#tabtop",
            "menu_url": "https://www.zomato.com/london/dishoom-covent-garden/menu?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1&openSwipeBox=menu&showMinimal=1#tabtop",
            "featured_image": "https://b.zmtcdn.com/data/res_imagery/6103211_RESTAURANT_19f7b41684765da1a700c2818f16cde0.jpg",
            "has_online_delivery": 0,
            "is_delivering_now": 0,
            "include_bogo_offers": true,
            "deeplink": "zomato://restaurant/6103211",
            "has_table_booking": 0,
            "events_url": "https://www.zomato.com/london/dishoom-covent-garden/events#tabtop?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1",
            "establishment_types": []
        }
    }]
}
于 2018-05-21T20:37:06.040 回答
0

第一件事是the JSON is invalid。我假设您可以管理有效的 JSON。验证 JSON 后。

首先创建一个 Restaurant POJO 类。

餐厅类的属性:

R: obj
apikey: string
location: obj
.... like .....

添加您需要的所有字段。

之后,使用代码进行解析。array但首先,从 JSON 中取出餐馆。

gson.fromJson(restaurants, Restaurant[].class);
于 2018-05-21T19:16:51.217 回答