Json data format in C # to parse the world map by using geocoding Newtonsoft.Json library (GeoCoder) service returned interface, and set the HttpWebRequest incomplete when the server returns "remote server returned an error: (403) Forbidden" Solution

FIG geocoding world (the GeoCoder) Json data format returned from the service interface, as follows:

HTTP: // api.tianditu.gov.cn/geocoder?ds={"keyWord ":" Beijing "} & tk = tk your key

Examples of results returned:

{
"msg":"ok",
"location":
                {
                    "level":"地名地址",
                    "lon":116.40100299989,
                    "lat":39.90311700025,
                    "keyWord":"北京市"
                },
"searchVersion":"6.0.0",
"status":"0"
}

This article briefly describes how to parse Json data format by using Newtonsoft.Json library in C #.

1, to create the main application (console or Winform program available);

2, is introduced Newtonsoft.Json.dll.

3, create the following classes in the project, add using Newtonsoft.Json; namespace references:

using System;
using System.Collections.Generic;
using System.Text;

using Newtonsoft.Json;

namespace WorldWind
{
    public class LocationTDT
    {
        [JsonProperty("level")]
        public string Level { get; set; }

        [JsonProperty("lon")]
        public double Lon { get; set; }

        [JsonProperty("lat")]
        public double Lat { get; set; }

        [JsonProperty("keyWord")]
        public string KeyWord { get; set; }
    }


    class RecordResultTDT
    {
        [JsonProperty("msg")]
        public string Msg { get; set; }

        [JsonProperty("location")]
        public LocationTDT Location { get; set; }

        [JsonProperty("searchVersion")]
        public string SearchVersion { get; set; }

        [JsonProperty("status")]
        public int Status { get; set; }
    }
}

4, constructed URL service connection, initiate Http request for acquiring the content data stream response, call JsonConvert.DeserializeObject () method of the string to deserialize objects, CS calling file needs to be added using Newtonsoft.Json; Name space references:

                    // Send the request 
                    the HttpWebRequest Request = (the HttpWebRequest) WebRequest.Create (m_CurrentSearchUri);
                     // The following code must be added, in a world map site would happen "remote server returned an error: (403) has banned" 
                    request.method = " the GET " ; 
                    request.Accept = " text / HTML, file application / XHTML + XML, file application / XML; Q = 0.9, * / *; Q = 0.8 " ; 
                    request.Headers.Add ( " the Accept-Language " , " ZH -CN, ZH; Q = 0.8, EN-US; Q = 0.5, EN; Q = 0.3 " ); 
                    request.UserAgent = "Mozilla / 5.0 (Windows NT 5.2; rv: 12.0) Gecko / Firefox 20,100,101 / 12.0 " ; 

                    // get response 
                    HttpWebResponse the Response = (HttpWebResponse) request.GetResponse (); 
                    StreamReader SR = new new StreamReader (response.GetResponseStream ());
                     / / to give a json string 
                    string joResultTemp = sr.ReadToEnd () the ToString ();.
                     // the returned data into json JSON object 
                    RecordResultTDT joResult = JsonConvert.DeserializeObject <RecordResultTDT> (joResultTemp); // return the world map Json objects

 5. In addition, the world map of the URL request will be returned when the browser directly input the correct Json result, but when set HttpWebRequest in C # application is not complete when the server returns "remote server returned an error: (403) Forbidden"

  Solution:

  As mentioned above in step 4, HttpWebRequest the Method, Accept, Headers, UserAgent and other parameters must be set to full. Do not set these parameters when you visit Baidu Maps geocoding (GeoCoder) service interface can also be a normal visit.

 

Guess you like

Origin www.cnblogs.com/rainbow70626/p/12353400.html