0

我无法从 Yelp API 解析 JSON。JSON 数据如下所示:

{
    region: {
        span: {
            latitude_delta: 0,
            longitude_delta: 0
        },
        center: {
            latitude: 38.054117,
            longitude: -84.439002
        }
    },
    total: 23,
    businesses: [
        {
            is_claimed: false,
            rating: 5,
            mobile_url: "http://m.yelp.com/biz/vineyard-community-church-lexington",
            rating_img_url: "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/f1def11e4e79/ico/stars/v1/stars_5.png",
            review_count: 2,
            name: "Vineyard Community Church",
            snippet_image_url: "http://s3-media4.ak.yelpcdn.com/photo/VoeMtbk7NRFi6diksSUtOQ/ms.jpg",
            rating_img_url_small: "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/c7623205d5cd/ico/stars/v1/stars_small_5.png",
            url: "http://www.yelp.com/biz/vineyard-community-church-lexington",
            phone: "8592582300",
            snippet_text: "I have been a member of Vineyard Community Church since 2004. Here you will find a modern worship service with a full band, witty speakers who teach...",
            image_url: "http://s3-media3.ak.yelpcdn.com/bphoto/D71eikniuaHjdOC8DB6ziA/ms.jpg",
            categories: [
                [
                    "Churches",
                    "churches"
                ]
            ],
            display_phone: "+1-859-258-2300",
            rating_img_url_large: "http://s3-media3.ak.yelpcdn.com/assets/2/www/img/22affc4e6c38/ico/stars/v1/stars_large_5.png",
            id: "vineyard-community-church-lexington",
            is_closed: false,
            location: {
                city: "Lexington",
                display_address: [
                    "1881 Eastland Pwky",
                    "Lexington, KY 40505"
                ],
                geo_accuracy: 8,
                postal_code: "40505",
                country_code: "US",
                address: [
                    "1881 Eastland Pwky"
                ],
                coordinate: {
                    latitude: 38.054117,
                    longitude: -84.439002
                },
                state_code: "KY"
            }
        }
    ]
}

JSON 存储在一个名为 @stuff 的 ruby​​ 字符串中,这是我用来尝试解析它的代码:

@parsed_stuff = JSON::parse(@stuff)

当我这样做并尝试显示 @ parsed_stuff 的内容时,我在浏览器中收到以下错误

Parse error on line 2: { "region"=>{ "span -------------^ Expecting '}', ':', ',', ']'

对此问题提供的任何帮助将不胜感激。

4

1 回答 1

0

使用jsonlint验证JSON。在这里,您必须将所有键作为字符串。

试试看

{
    "region": {
        "span": {
            "latitude_delta": 0,
            "longitude_delta": 0
        },
        "center": {
            "latitude": 38.054117,
            "longitude": -84.439002
        }
    },
    "total": 23,
    "businesses": [
        {
            "is_claimed": false,
            "rating": 5,
            "mobile_url": "http://m.yelp.com/biz/vineyard-community-church-lexington",
            "rating_img_url": "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/f1def11e4e79/ico/stars/v1/stars_5.png",
            "review_count": 2,
            "name": "Vineyard Community Church",
            "snippet_image_url": "http://s3-media4.ak.yelpcdn.com/photo/VoeMtbk7NRFi6diksSUtOQ/ms.jpg",
            "rating_img_url_small": "http://s3-media1.ak.yelpcdn.com/assets/2/www/img/c7623205d5cd/ico/stars/v1/stars_small_5.png",
            "url": "http://www.yelp.com/biz/vineyard-community-church-lexington",
            "phone": "8592582300",
            "snippet_text": "I have been a member of Vineyard Community Church since 2004. Here you will find a modern worship service with a full band, witty speakers who teach...",
            "image_url": "http://s3-media3.ak.yelpcdn.com/bphoto/D71eikniuaHjdOC8DB6ziA/ms.jpg",
            "categories": [
                [
                    "Churches",
                    "churches"
                ]
            ],
            "display_phone": "+1-859-258-2300",
            "rating_img_url_large": "http://s3-media3.ak.yelpcdn.com/assets/2/www/img/22affc4e6c38/ico/stars/v1/stars_large_5.png",
            "id": "vineyard-community-church-lexington",
            "is_closed": false,
            "location": {
                "city": "Lexington",
                "display_address": [
                    "1881 Eastland Pwky",
                    "Lexington, KY 40505"
                ],
                "geo_accuracy": 8,
                "postal_code": "40505",
                "country_code": "US",
                "address": [
                    "1881 Eastland Pwky"
                ],
                "coordinate": {
                    "latitude": 38.054117,
                    "longitude": -84.439002
                },
                "state_code": "KY"
            }
        }
    ]
}
于 2013-01-11T19:02:01.410 回答