我有一个天气应用程序,可以持续接收JSON格式的天气相关信息。到目前为止,我正在使用Newtonsoft来检索和解析响应。不幸的是,我时不时会收到一个错误,确实令人困惑。
代码运行正常,但是几个小时后,我的应用程序引发异常并显示此错误:
render() {
let data = [{ "name": "test1" }, { "name": "test2" }];
return (
<>
{data.map(d => <span key={d.name}>{d.name}</span>}
</>
);
}}
JSON结构
Error reading JObject from JsonReader. Path '', line 0, position 0.
代码
{
"weather": {
"city": "Seattle"
"temperature": "20"
"degrees": "C"
},
}
当我想获取城市,温度或度数时,这就是我的方法=>
public static JObject GetWeather(string url)
{
var client = new RestClient(url);
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
var response = client.Execute(request);
var content = response.Content;
var result = JObject.Parse(content);
return result;
}
,
var city = result["weather"]["city"].ToString()
,
var temperature = result["weather"]["temperature"].ToString()
我要去哪里错了?