Eliminate weird characters in Json string when calling the API by using C#?

Prageeth Liyanage

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.

API for the TAGS

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.

MartijnK

당신이 받고있는 것은 압축 된 응답입니다. 따라서 문자열로 읽는 대신 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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Weird messages when using sudo

분류에서Dev

Separating a string using predefined characters

분류에서Dev

Calling by skype using C#

분류에서Dev

How to limit string characters when rendered in MeteorJS

분류에서Dev

Weird behaviour in calling template function

분류에서Dev

Weird crash when using PIL to save screen and display by PYQT

분류에서Dev

Character is moving in weird ways when using Transform.forward in Unity

분류에서Dev

How to assign JSON array to a string using the SimpleJSON library in C++

분류에서Dev

C # Calling Salesforce Rest Api with custom header

분류에서Dev

C# null reference error when using a global string variable

분류에서Dev

How to remove duplicate characters in a string using regex?

분류에서Dev

Urlencoding weird characters in another charset

분류에서Dev

Objective-C: Randomly replace characters in string

분류에서Dev

storing string and characters in two dimensional array (C)

분류에서Dev

C++ String Subcript Out of Range when using string.length() as end of for loop

분류에서Dev

Using NCHAR to get special characters in C#

분류에서Dev

Calling a function using a string containing the function's name

분류에서Dev

How to eliminate Vowels from a string in java?

분류에서Dev

How to eliminate certain elements when scraping?

분류에서Dev

Ignore first 2 characters of string in where query using active record

분류에서Dev

Using PHP, search a string of characters for a URL and save that URL as a value

분류에서Dev

Using the reduce method to eliminate any duplicated numbers

분류에서Dev

Turn string into json C#

분류에서Dev

Json Schema Validation in Asp.Net Web api using C#

분류에서Dev

How to remove unsafe characters from string for logging in C#

분류에서Dev

Replacing a character in a string (char array) with multiple characters in C

분류에서Dev

Weird (?) behavior when comparing iterators

분류에서Dev

Input string without using C++ string

분류에서Dev

Get substring of string using C

Related 관련 기사

  1. 1

    Weird messages when using sudo

  2. 2

    Separating a string using predefined characters

  3. 3

    Calling by skype using C#

  4. 4

    How to limit string characters when rendered in MeteorJS

  5. 5

    Weird behaviour in calling template function

  6. 6

    Weird crash when using PIL to save screen and display by PYQT

  7. 7

    Character is moving in weird ways when using Transform.forward in Unity

  8. 8

    How to assign JSON array to a string using the SimpleJSON library in C++

  9. 9

    C # Calling Salesforce Rest Api with custom header

  10. 10

    C# null reference error when using a global string variable

  11. 11

    How to remove duplicate characters in a string using regex?

  12. 12

    Urlencoding weird characters in another charset

  13. 13

    Objective-C: Randomly replace characters in string

  14. 14

    storing string and characters in two dimensional array (C)

  15. 15

    C++ String Subcript Out of Range when using string.length() as end of for loop

  16. 16

    Using NCHAR to get special characters in C#

  17. 17

    Calling a function using a string containing the function's name

  18. 18

    How to eliminate Vowels from a string in java?

  19. 19

    How to eliminate certain elements when scraping?

  20. 20

    Ignore first 2 characters of string in where query using active record

  21. 21

    Using PHP, search a string of characters for a URL and save that URL as a value

  22. 22

    Using the reduce method to eliminate any duplicated numbers

  23. 23

    Turn string into json C#

  24. 24

    Json Schema Validation in Asp.Net Web api using C#

  25. 25

    How to remove unsafe characters from string for logging in C#

  26. 26

    Replacing a character in a string (char array) with multiple characters in C

  27. 27

    Weird (?) behavior when comparing iterators

  28. 28

    Input string without using C++ string

  29. 29

    Get substring of string using C

뜨겁다태그

보관