【问题标题】:json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 1) errorjson.decoder.JSONDecodeError:预期值:第 2 行第 1 列(字符 1)错误
【发布时间】:2018-09-18 07:11:34
【问题描述】:
        f = urlopen('http://api.wunderground.com/api/API_KEY_HERE/geolookup/conditions/q/CA/LosAngeles.json')
        str_response = f.readline().decode('utf-8')
        parsed_json = json.loads(str_response)
        location = parsed_json['location']['city']
        temp_f = parsed_json['current_observation']['temp_f']
        print ("Current temperature is:", temp_f, " degrees Fahrenheit")
        precep = parsed_json['current_observation']['precep_today_in']
        print("Current wind speed is:", precep)
        wind = parsed_json['current_observation']['wind_mph']
        print("Current wind speed is:", wind)
        gust = parsed_json['current_observation']['wind_gust_mph']
        print("Current wind gust speed:", gust)
        f.close()

我看到了这个JSONDecodeError: Expecting value: line 1 column 1,但我无法弄清楚如何添加这段代码。

这是错误:

这是我要求的 API 部分:

relative_humidity   "81%"
wind_string "Calm"
wind_dir    "NNE"
wind_degrees    23
wind_mph    0
wind_gust_mph   0
wind_kph    0
wind_gust_kph   0
pressure_mb "1012"
pressure_in "29.87"
pressure_trend  "-"
dewpoint_string "53 F (12 C)"
dewpoint_f  53
dewpoint_c  12
heat_index_string   "NA"
heat_index_f    "NA"
heat_index_c    "NA"
windchill_string    "NA"
windchill_f "NA"
windchill_c "NA"

【问题讨论】:

  • 请显示您尝试解析的 JSON 的一小部分

标签: python json python-3.x wunderground


【解决方案1】:

使用f.readline(),您的代码只会读取 API 返回的第一行,这恰好是一个空行,因此 JSON 编码器会抱怨没有数据。

f.readline().decode('utf-8') 更改为f.read().decode('utf-8'),您应该可以克服这个错误。

【讨论】:

    猜你喜欢
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 2019-09-10
    • 2016-03-03
    • 2020-08-16
    • 2019-06-10
    • 1970-01-01
    • 2019-11-06
    相关资源
    最近更新 更多