1

I have the following JSON:

{
"request_time": "2017-01-01T20:21:26+00:00",
"stations": [
{
  "station_code": "MAT",
  "atcocode": null,
  "tiploc_code": "MATLOCK",
  "name": "Matlock",
  "mode": "train",
  "longitude": -1.558911,
  "latitude": 53.138406,
  "distance": 221
},
{
  "station_code": "MTB",
  "atcocode": null,
  "tiploc_code": "MATLCKB",
  "name": "Matlock Bath",
  "mode": "train",
  "longitude": -1.556759,
  "latitude": 53.122353,
  "distance": 1784
   }
 ]
}

I am trying to pin the station names, longitude and latitude to a google map. I am getting a error but with no description of where the error is. This is the back end C# code:

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!ClientScript.IsStartupScriptRegistered("window"))
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "window", "CreateMarker();", true);
        }

    }

    [WebMethod]
    public MAPS[] BindMapMarker()
    {

        DataTable dt = new DataTable();
        DataRow dr;
        List<MAPS> lstMarkers = new List<MAPS>();
        try
        {

            string json = get_web_content("https://transportapi.com/v3/uk/train/stations/near.json?app_id=03bf8009&app_key=d9307fd91b0247c607e098d5effedc97&lat=53.13838&lon=-1.5556&rpp=3");

            dynamic array = JsonConvert.DeserializeObject(json);
            dynamic DepartTimes = array.stations;

            foreach (var item in DepartTimes)
            {
                //dt.Columns.Add("LocationName");
                dt.Columns.Add("Location");
                dt.Columns.Add("Latitude");
                dt.Columns.Add("Longitude");

                dr = dt.NewRow();
                dr["Location"] = item["name"];
                dr["Latitude"] = item["latitude"];
                dr["Longitude"] = item["longitude"];


                dt.Rows.Add(dr);

            }



            foreach (DataRow dtrow in dt.Rows)
            {
                MAPS objMAPS = new MAPS();
                objMAPS.LocationName = dtrow[0].ToString();
                objMAPS.Latitude = dtrow[0].ToString();
                objMAPS.Longitude = dtrow[1].ToString();
                lstMarkers.Add(objMAPS);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }


        return lstMarkers.ToArray();
    }



    public class MAPS
    {
        public string LocationName;
        public string Latitude;
        public string Longitude;
    }

    public string get_web_content(string url)
    {
        Uri uri = new Uri(url);
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
        request.Method = WebRequestMethods.Http.Get;
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        StreamReader reader = new StreamReader(response.GetResponseStream());
        string output = reader.ReadToEnd();
        response.Close();

        return output;
    }

}

Thanks!

6
  • 1
    One thing that might be blowing up is that you are creating your columns multiple times by putting that code in the body of the foreach loop. You should only be adding these once before processing the results from the API. However, why are you putting them in a DataTable only to transfer them to a collection of objects just below there? Commented Jan 1, 2017 at 21:43
  • I am putting them into a data table in order to add each row of data to a google map, is it not necessary to add the JSON data to a DataTable? Commented Jan 1, 2017 at 21:52
  • 1
    Where are you adding them to a Google map? In the JavaScript that processes the results of the web method? If so, then no, adding them to a DataTable is an unnecessary middle step. Just use one foreach loop to assign the data in DepartTimes straight to MAP objects and return the list of those. Commented Jan 1, 2017 at 21:59
  • Okay thanks for your help! Commented Jan 1, 2017 at 22:05
  • You're welcome. If this solved your problem, please accept the answer I added below. Many thanks! Commented Jan 1, 2017 at 22:08

2 Answers 2

1

One thing that might be blowing up is that you are creating your columns multiple times by putting that code in the body of the foreach loop. You should only be adding these once before processing the results from the API.

Where are you adding them to a Google map? In the JavaScript that processes the results of the web method? If so, then no, adding them to a DataTable is an unnecessary middle step. Just use one foreach loop to assign the data in DepartTimes straight to MAP objects and return the list of those.

Sign up to request clarification or add additional context in comments.

Comments

0

something like this then?

 foreach (var item in DepartTimes)
            {
                MAPS objMAPS = new MAPS();
                objMAPS.LocationName = item["name"].ToString();
                objMAPS.Latitude = item["latitude"].ToString();
                objMAPS.Longitude = item["longitude"].ToString();
                lstMarkers.Add(objMAPS);
             }

1 Comment

Yup that's what I was thinking.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.