Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# JSON对象抛出空响应,但它不是';t空_C#_Asp.net_Json - Fatal编程技术网

C# JSON对象抛出空响应,但它不是';t空

C# JSON对象抛出空响应,但它不是';t空,c#,asp.net,json,C#,Asp.net,Json,尝试使用JavaScriptSerializer()解析JSON对象。反序列化,但在foreach(getRoute.results中的var项)处抛出空错误,其中Route getRoute=new JavaScriptSerializer()。反序列化(strresulttest) 问题是,当我通过打印内容调试strresulttest时,我能够看到预期的JSON输出,如图所示 解析JSON的代码: public class Route { public L

尝试使用
JavaScriptSerializer()解析JSON对象。反序列化
,但在
foreach(getRoute.results中的var项)
处抛出空错误,其中
Route getRoute=new JavaScriptSerializer()。反序列化(strresulttest)

问题是,当我通过打印内容调试strresulttest时,我能够看到预期的JSON输出,如图所示

解析JSON的代码:

     public class Route
    {
        public List<GetRoute> results { get; set; }
    }

    public class GetRoute
    {
        public string status_message { get; set; }
        public string viaRoute { get; set; }
        public string subtitle { get; set; }
        public int totalTime { get; set; }
        public int totalDistance { get; set; }
    }

    private void GetInstructions()
    {
        //GET METHOD for route query
        string strurltest = String.Format("https://developers.onemap.sg/privateapi/routingsvc/route?start="+
            startLat+","+ startLon +"&end="+ destinationLat +","+ destinationLon+"&"+
            "routeType="+ transportType + "&token="+token);
        WebRequest requestObjGet = WebRequest.Create(strurltest);
        requestObjGet.Method = "GET";
        HttpWebResponse responseObjGet = null;
        responseObjGet = (HttpWebResponse)requestObjGet.GetResponse();
        string strresulttest = null;
        using (Stream stream = responseObjGet.GetResponseStream())
        {
            StreamReader sr = new StreamReader(stream);
            strresulttest = sr.ReadToEnd();
            //reminder: remove after prod. GET is working.
            System.Diagnostics.Debug.WriteLine(strresulttest);
            sr.Close();
        }

        //display search recommendations
        Route getRoute = new JavaScriptSerializer().Deserialize<Route>(strresulttest);
        foreach (var item in getRoute.results)
        {
                //reminder: remove after prod. 
                System.Diagnostics.Debug.WriteLine("Route via: " + item.viaRoute + "\n");
                System.Diagnostics.Debug.WriteLine("Description: " + item.subtitle + "\n");
        }
    }

由于反序列化到的类结构与JSON不匹配,因此
结果中的值为空。您在问题中链接的JSON对应于此类结构:

public class Route
{
    public string status_message { get; set; }
    public string route_geometry { get; set; }
    public int status { get; set; }
    public List<List<object>> route_instructions { get; set; }
    public List<string> route_name { get; set; }
    public RouteSummary route_summary { get; set; }
    public string viaRoute { get; set; }
    public string subtitle { get; set; }
    public Phyroute phyroute { get; set; }
}

public class RouteSummary
{
    public string start_point { get; set; }
    public string end_point { get; set; }
    public int total_time { get; set; }
    public int total_distance { get; set; }
}

public class Phyroute
{
    public string status_message { get; set; }
    public string route_geometry { get; set; }
    public int status { get; set; }
    public List<List<object>> route_instructions { get; set; }
    public List<string> route_name { get; set; }
    public RouteSummary route_summary { get; set; }
    public string viaRoute { get; set; }
    public string subtitle { get; set; }
}

“我能够看到我预期的JSON输出,如图所示”您的整个问题(包括任何必要的代码)必须在您的问题中,而不仅仅是链接。原因有两个:人们不应该非得去场外帮助你;链接腐烂,使问题及其答案对未来的人们毫无用处。请将所有相关信息,包括JSON结构的一部分,放在问题中。更多:您的响应是
对象
而不是
数组
so
GetRoute route=JavaScriptSerializer()。反序列化(strresulttest)
public class Route
{
    public string status_message { get; set; }
    public string route_geometry { get; set; }
    public int status { get; set; }
    public List<List<object>> route_instructions { get; set; }
    public List<string> route_name { get; set; }
    public RouteSummary route_summary { get; set; }
    public string viaRoute { get; set; }
    public string subtitle { get; set; }
    public Phyroute phyroute { get; set; }
}

public class RouteSummary
{
    public string start_point { get; set; }
    public string end_point { get; set; }
    public int total_time { get; set; }
    public int total_distance { get; set; }
}

public class Phyroute
{
    public string status_message { get; set; }
    public string route_geometry { get; set; }
    public int status { get; set; }
    public List<List<object>> route_instructions { get; set; }
    public List<string> route_name { get; set; }
    public RouteSummary route_summary { get; set; }
    public string viaRoute { get; set; }
    public string subtitle { get; set; }
}
Route route = new JavaScriptSerializer().Deserialize<Route>(strresulttest);
System.Diagnostics.Debug.WriteLine("Route via: " + route.viaRoute);
System.Diagnostics.Debug.WriteLine("Description: " + route.subtitle);