简体   繁体   English

错误 - JObject 解析错误,响应为 []

[英]Error - JObject Parse Error with [] in response

Trying to parse:试图解析:

[{"place_id":84979036,"licence":"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright","powered_by":"Map Maker: https://maps.co","osm_type":"node","osm_id":8358755414,"boundingbox":["35.8497801","35.8897801","-114.6800654","-114.6400654"],"lat":"35.8697801","lon":"-114.6600654","display_name":"Willow Beach, Mohave County, Arizona, United States","class":"place","type":"village","importance":0.47500000000000003}]

But the boundingbox:[] in the statement are causing errors, I can't get rid of them so I need to find a way to pull the lat and lon without getting this error.但是语句中的 boundingbox:[] 导致错误,我无法摆脱它们,所以我需要找到一种方法来拉纬度和经度而不出现此错误。

Error:错误:

Newtonsoft.Json.JsonReaderException: 'Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.'

Code:代码:

bool PullGeoJson(string city, string state, out string[] output)
{
    HttpResponse<string> response = UnirestLocationRequest(city, state);
    Console.WriteLine(response.Body.ToString());
    JObject json = JObject.Parse(response.Body.ToString());
    output = new string[2];
    output[0] = json.GetValue("lat").ToString();
    output[1] = json.GetValue("lon").ToString();
    return !(output == null);
}

Create a model that contains the structure of the response you're expecting then deserialize into the model:创建一个包含您期望的响应结构的 model,然后反序列化为 model:

static void Main(string[] args)
{
    string city = "Pense";
    string state = "Canada";
    HttpResponse<string> response = Unirest.get("https://geocode.maps.co/search?q=" + city + "," + state).asJson<string>();

    var data = JsonConvert.DeserializeObject<List<GeoModel>>(response.Body.ToString());
}

public partial class GeoModel
{
    [JsonProperty("place_id")]
    public long PlaceId { get; set; }

    [JsonProperty("lat")]
    public string Lat { get; set; }

    [JsonProperty("lon")]
    public string Lon { get; set; }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM