I am developing an website for my company and in there I want to tag skills to specific people. So the programming tags that shows in stackoverflow is a valuable source. SO I want to get the tag db of the stackoverflow.
I found an API for that.
So what I am trying to do is read this json string and forloop through the pages to get the tags and save them in a DB.
private static void ReadJson()
{
HttpClient client = new HttpClient();
//DefaultRequestHeader to Json
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//Create an instance of HttpResponse & invoke the service asynchronously
HttpResponseMessage response = client.GetAsync("https://api.stackexchange.com/2.2/tags?page=400&pagesize=100&order=desc&sort=popular&site=stackoverflow").Result;
//Http Status code 200
if (response.IsSuccessStatusCode)
{
//Read response content result into string variable
string JSON = response.Content.ReadAsStringAsync().Result;
//Deserialize the string(JSON) object
var jObj = (JObject)JsonConvert.DeserializeObject(JSON);
//access items from anonymous (Json object) type and add to the list
var result = jObj["items"].Select(item => new
{
name = item["name"]
}).ToList();
//output the data || NOTE: **NSERT into database table**
foreach (var item in result)
{
Console.WriteLine(item.name);
}
}
}
So at string JSON = response.Content.ReadAsStringAsync().Result;
method it shows some weired charachters ( triangels and shapes ) because of that process is stopping there.
0\0\0�Z�n�8\f��<�E/S��,-�cYtuI�\f�ߗf\a�g�
What I am doing wrong here?
If there any way to make this happen please contribute your answer.
Thanks.
당신이 받고있는 것은 압축 된 응답입니다. 따라서 문자열로 읽는 대신 byte []로 읽고 압축을 풀면 JSON 문자열을 찾을 수 있습니다.
static async void DoStuff()
{
HttpClient client = new HttpClient();
var bytes = await client.GetByteArrayAsync("https://api.stackexchange.com/2.2/tags?page=400&pagesize=100&order=desc&sort=popular&site=stackoverflow");
var decompressedJson = new StreamReader(new GZipStream(new MemoryStream(bytes), CompressionMode.Decompress)).ReadToEnd();
// decompressedJson will now contain '{"items":[{"has_synonyms":false, .....'
// Continue with deserialization of JSON
}
static void Main(string[] args)
{
Task t = new Task(DoStuff);
t.Start();
Console.WriteLine("Doing stuff");
Console.ReadLine();
}
}
거기에서 역 직렬화를 계속할 수 있습니다. 너무 많은 요청을 보내면 API에서 오류가 발생합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다