Парсинг через WebClient - C#
Формулировка задачи:
Мне нужно парсить эту страницу http://ratings.tankionline.com/get_stat/profile/?user=MyLove_MySecret
Вот так я достаю значение kills (также deaths, и т.д.):
Но мне еще нужно парсить это:
Отсюда мне надо достать значение position и value в отдельные переменные
Листинг программы
- "kills":15151,
Листинг программы
- WebClient client = new WebClient();
- var myRaiting = new WebClient().DownloadString(
- string.Format("http://ratings.tankionline.com/get_stat/profile/?user={0}", Login.Text.ToLower()));
- ...
- Kill.Text = (myRaiting.Split(new[] { "kills":" }, StringSplitOptions.None)[1].Split(',')[0]);
Листинг программы
- "rating":{"score":{"position":74194,"value":986},
Решение задачи: «Парсинг через WebClient»
textual
Листинг программы
- static void Main(string[] args)
- {
- string json = @"""rating"":{""score"":{""position"":74194,""value"":986},";
- /* Моя ф-ция парсинга */
- string position = PARS.GetPars(json, @"position"":", @",");
- string value = PARS.GetPars(json, @"value"":", "}");
- Console.WriteLine("MY STRUCTURE - POSITION: " + position + " VALUE: " + value);
- /* Ф-ция Split */
- position = json.Split(':')[3].Split(',')[0];
- value = json.Split(':')[4].Split('}')[0];
- Console.WriteLine("USE SPLIT - POSITION: " + position + " VALUE: " + value);
- Console.ReadLine();
- }
- struct PARS
- {
- public static string GetPars(string strSource, string strStart, string strEnd, int Index = 0, int startPos = 0)
- {
- int iPos = 0;
- int iEnd = 0;
- int lenStart = strStart.Length;
- string strResult = null;
- strResult = string.Empty;
- for (int i = 0; i <= Index - 1; i += 1)
- {
- iPos = strSource.IndexOf(strStart, startPos);
- iEnd = strSource.IndexOf(strEnd, iPos + lenStart);
- iEnd = iEnd + strEnd.Length;
- if (iPos != -1 && iEnd != -1)
- {
- strSource = strSource.Substring(iEnd, strSource.Length - (0 + iEnd));
- }
- else {
- break;
- }
- }
- iPos = strSource.IndexOf(strStart, startPos);
- iEnd = strSource.IndexOf(strEnd, iPos + lenStart);
- if (iPos != -1 && iEnd != -1)
- {
- strResult = strSource.Substring(iPos + lenStart, iEnd - (iPos + lenStart));
- }
- return strResult;
- }
- public static int Count(string strSource, string strStart, string strEnd, int startPos = 0)
- {
- int iPos = 0;
- int iEnd = 0;
- int lenStart = strStart.Length;
- int IntResult = 0;
- bool BooL = false;
- while (BooL == false)
- {
- iPos = strSource.IndexOf(strStart, startPos);
- iEnd = strSource.IndexOf(strEnd, iPos + lenStart);
- if (iPos != -1 && iEnd != -1)
- {
- IntResult = IntResult + 1;
- startPos = iEnd + 1;
- }
- else {
- BooL = true;
- }
- }
- return IntResult;
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д