【问题标题】:Error Parsing JSON from YELP API从 YELP API 解析 JSON 时出错
【发布时间】:2013-01-11 16:30:12
【问题描述】:

我无法从 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 '}', ':', ',', ']'

我们将非常感谢您在此问题上提供的任何帮助。

【问题讨论】:

    标签: ruby json parsing yelp


    【解决方案1】:

    使用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"
                }
            }
        ]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-03
      • 1970-01-01
      • 1970-01-01
      • 2015-07-06
      • 1970-01-01
      • 2018-11-06
      相关资源
      最近更新 更多