REST服务 - JSON反序列化错误:遇到意外字符'<'

时间:2012-09-01 14:36:13

标签: rest json.net deserialization bing-maps

    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);
        }
    }

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

我的上述代码有什么问题吗?

2 个答案:

答案 0 :(得分:5)

只需更改此行即可使用JSON而不是XML格式进行查询

所以从这......

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

对此...

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

即。只需将output=xml更改为output=json

即可

答案 1 :(得分:0)

我将代码中的未定义RootObject替换为Bing Maps REST Service .NET Libraries中的BingMapsRESTService.Common.JSON.Response类型,而@rbrundritt建议您使用in your other post