简体   繁体   中英

JSON Parsing to retrieve and display the values of attributes in a textbox (WIndows Phone 8)

I am trying to parse a JSON from a URL and retrieve it's data value of each attribute and display it in a textbox. But i am facing this error: "An exception of type 'Newtonsoft.Json.JsonReaderException' occurred in Newtonsoft.Json.DLL but was not handled in user code" Error reading integer. Unexpected token: StartArray. Path 'BusRoute[0].STEPS

This are my codes:

         // Create webclient.
         WebClient client = new WebClient();

         client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
         client.DownloadStringAsync(new Uri("http://www.onemap.sg/publictransportation/service1.svc/routesolns?token=qo/s2TnSUmfLz+32CvLC4RMVkzEFYjxqyti1KhByvEacEdMWBpCuSSQ+IFRT84QjGPBCuz/cBom8PfSm3GjEsGc8PkdEEOEr&sl=39167.4524,35518.8625&el=28987.5163,33530.5653&startstop=&endstop=&walkdist=300&mode=bus&routeopt=cheapest&retgeo=true&maxsolns=1&callback="));
    }

            /*public void loadHTMLCallback(Object sender, DownloadStringCompletedEventArgs e)
            {
            string content = e.Result;
            tb_json.Text = content ;
            }*/


            public class BusRoute
            {
            public int SOLUTION { get; set; }
            public string DURATION { get; set; }
            public string TOTALCARD { get; set; }
            public string TOTALCASH { get; set; }
            public string TOTALDISTANCE { get; set; }
            public int STEPS { get; set; }
            public string TYPE { get; set; }
            public string SERVICETYPE { get; set; }
            public string SERVICEID { get; set; }
            public string NUMBEROFSTOP { get; set; }
            public string BOARDID { get; set; }
            public string BOARDDESC { get; set; }
            public string BOARDDIST { get; set; }
            public string ALIGHTID { get; set; }
            public string ALIGHTDESC { get; set; }
            public string ALIGHTDIST { get; set; }
            public int TOTALSTOPS { get; set; }
            public string PATH { get; set; }

            }

            public class RootObject
            {
            public List<BusRoute> BusRoute { get; set; }
            }

            void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
            {
                var rootObject = JsonConvert.DeserializeObject<RootObject>(e.Result);
                foreach (var route in rootObject.BusRoute)
            {
             int t = route.TOTALSTOPS;
             string totalStops = t.ToString();
             tb_test.Text = "Total number of stops: " + totalStops;
            }
         }

Please give me some guidance on how I can retrieve the attributes and display it in the textbox. Thank you!

i tried using your json url: http://www.onemap.sg/publictransportation/service1.svc/routesolns?token=qo/s2TnSUmfLz+32CvLC4RMVkzEFYjxqyti1KhByvEacEdMWBpCuSSQ+IFRT84QjGPBCuz/cBom8PfSm3GjEsGc8PkdEEOEr&sl=39167.4524,35518.8625&el=28987.5163,33530.5653&startstop=&endstop=&walkdist=300&mode=bus&routeopt=cheapest&retgeo=true&maxsolns=1&callback=

(is the token safe to be put here on public internet?)

Generated class from http://json2csharp.com

public class STEP
{
    public string STEP { get; set; }
    public string type { get; set; }
    public string ServiceType { get; set; }
    public string ServiceID { get; set; }
    public string NumberOfStop { get; set; }
    public string BoardId { get; set; }
    public string BoardDesc { get; set; }
    public string BoardDist { get; set; }
    public string AlightId { get; set; }
    public string AlightDesc { get; set; }
    public string AlightDist { get; set; }
}

public class BusRoute
{
    public string Solution { get; set; }
    public string Duration { get; set; }
    public string TotalCard { get; set; }
    public string TotalCash { get; set; }
    public string TotalDistance { get; set; }
    public List<STEP> STEPS { get; set; }
    public string TotalStops { get; set; }
    public List<List<string>> PATH { get; set; }
}

public class RootObject
{
    public List<BusRoute> BusRoute { get; set; }
}



var rootObject = JsonConvert.DeserializeObject<RootObject>(e.Result); <- this works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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