das_tnr
das_tnr

Reputation: 151

REST Services - JSON deserialization error: Encountered unexpected character '<'

    public async void MakeRequest(string requestUrl)
    {
        try
        {
            HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
            using (HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse)
            {
                if (response.StatusCode != HttpStatusCode.OK)
                    throw new Exception(string.Format(
                    "Server error(HTTP {0}:{1}.",
                    response.StatusCode,
                    response.StatusDescription));
                DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(RootObject));
                object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
                root = (RootObject)objResponse;

                if (root == null)
                    this.Frame.Navigate(typeof(MainPage));
            }
        }
        catch (Exception ex)
        {
            ThrowException(ex);
        }
    }

Link : http://dev.virtualearth.net/REST/V1/Routes?wp.0=39.920829,32.853883&wp.1=39.877666,32.864728&key=BingMapsKey

Is there anything wrong in my in the codes above?

Upvotes: 1

Views: 6608

Answers (2)

Mehdi Bugnard
Mehdi Bugnard

Reputation: 3979

Just change this line for make the query with JSON and not XML format

So from this ...

string query = "http://dev.virtualearth.net/REST/v1/Locations/" +         
               queryString + "?output=xml" + " &key=" + BingMapsKey;

To this ...

string query = "http://dev.virtualearth.net/REST/v1/Locations/" +
               queryString + "?output=json" + " &key=" + BingMapsKey;

i.e. just change output=xml to output=json

Upvotes: 5

Jim O'Neil
Jim O'Neil

Reputation: 23754

This code is working fine for me once I replace the undefined RootObject in your code with the BingMapsRESTService.Common.JSON.Response type found in the Bing Maps REST Service .NET Libraries that @rbrundritt suggested you use in your other post

Upvotes: 0

Related Questions