C# 处理google json请求和处理错误的正确方法

C# 处理google json请求和处理错误的正确方法,c#,json,json.net,C#,Json,Json.net,我正在使用restapi使用googlemaps从给定位置获取位置列表,但我遇到了一些问题 主要问题:虽然大部分时间响应正常,但有时我会收到格式错误的json或status=“Not ok”,这会导致我的应用程序崩溃: 在反序列化之前,是否有一种方法可以测试json的状态和有效性,以防止崩溃或其他方式 System.NullReferenceException: Object reference not set to an instance of an object. 从这一行: place

我正在使用restapi使用googlemaps从给定位置获取位置列表,但我遇到了一些问题

主要问题:虽然大部分时间响应正常,但有时我会收到格式错误的json或status=“Not ok”,这会导致我的应用程序崩溃:

在反序列化之前,是否有一种方法可以测试json的状态和有效性,以防止崩溃或其他方式

System.NullReferenceException: Object reference not set to an instance of an object.
从这一行:

place = JsonConvert.DeserializeObject<Places>(content,settings);
学额及班级:

//JsontoC#

public class Location
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Northeast
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Southwest
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Viewport
{
    public Northeast northeast { get; set; }
    public Southwest southwest { get; set; }
}

public class Geometry
{
    public Location location { get; set; }
    public Viewport viewport { get; set; }
}

public class OpeningHours
{
    public bool open_now { get; set; }
    public List<object> weekday_text { get; set; }
}

public class Photo
{
    public int height { get; set; }
    public List<string> html_attributions { get; set; }
    public string photo_reference { get; set; }
    public int width { get; set; }
}

public class Result
{
    public Geometry geometry { get; set; }
    public string icon { get; set; }
    public string id { get; set; }
    public string name { get; set; }
    public OpeningHours opening_hours { get; set; }
    public List<Photo> photos { get; set; }
    public string place_id { get; set; }
    public double rating { get; set; }
    public string reference { get; set; }
    public string scope { get; set; }
    public List<string> types { get; set; }
    public string vicinity { get; set; }
    public int? price_level { get; set; }
}

public class RootObject
{
    public List<object> html_attributions { get; set; }
    public string next_page_token { get; set; }
    public List<Result> results { get; set; }
    public string status { get; set; }
}
//JsontoC#
公共类位置
{
公共双lat{get;set;}
公共双精度{get;set;}
}
公共课东北
{
公共双lat{get;set;}
公共双精度{get;set;}
}
西南公务舱
{
公共双lat{get;set;}
公共双精度{get;set;}
}
公共类视口
{
公共东北{get;set;}
公共西南西南{get;set;}
}
公共课几何
{
公共位置位置{get;set;}
公共视口{get;set;}
}
公共课开放时间
{
公共bool open_now{get;set;}
公共列表工作日_text{get;set;}
}
公开课照片
{
公共整数高度{get;set;}
公共列表html_属性{get;set;}
公共字符串photo_引用{get;set;}
公共整数宽度{get;set;}
}
公开课成绩
{
公共几何体{get;set;}
公共字符串图标{get;set;}
公共字符串id{get;set;}
公共字符串名称{get;set;}
公共开放小时开放时间{get;set;}
公开列表照片{get;set;}
公共字符串place_id{get;set;}
公共双重评级{get;set;}
公共字符串引用{get;set;}
公共字符串作用域{get;set;}
公共列表类型{get;set;}
公共字符串{get;set;}
public int?price_level{get;set;}
}
公共类根对象
{
公共列表html_属性{get;set;}
公共字符串下一页标记{get;set;}
公共列表结果{get;set;}
公共字符串状态{get;set;}
}

您在这里问的问题太多了。每次问一个问题try/catch的可能副本应捕获异常。它可能会崩溃,因为在这种情况下,您返回null,并且您试图在其他位置访问它
{
   "error_message" : "The provided API key is invalid.",
   "html_attributions" : [],
   "results" : [],
   "status" : "REQUEST_DENIED"
}


{
   "html_attributions" : [],
   "results" : [],
   "status" : "INVALID_REQUEST"
}
//JsontoC#

public class Location
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Northeast
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Southwest
{
    public double lat { get; set; }
    public double lng { get; set; }
}

public class Viewport
{
    public Northeast northeast { get; set; }
    public Southwest southwest { get; set; }
}

public class Geometry
{
    public Location location { get; set; }
    public Viewport viewport { get; set; }
}

public class OpeningHours
{
    public bool open_now { get; set; }
    public List<object> weekday_text { get; set; }
}

public class Photo
{
    public int height { get; set; }
    public List<string> html_attributions { get; set; }
    public string photo_reference { get; set; }
    public int width { get; set; }
}

public class Result
{
    public Geometry geometry { get; set; }
    public string icon { get; set; }
    public string id { get; set; }
    public string name { get; set; }
    public OpeningHours opening_hours { get; set; }
    public List<Photo> photos { get; set; }
    public string place_id { get; set; }
    public double rating { get; set; }
    public string reference { get; set; }
    public string scope { get; set; }
    public List<string> types { get; set; }
    public string vicinity { get; set; }
    public int? price_level { get; set; }
}

public class RootObject
{
    public List<object> html_attributions { get; set; }
    public string next_page_token { get; set; }
    public List<Result> results { get; set; }
    public string status { get; set; }
}