Answer the question
In order to leave comments, you need to log in
How can I parse this kind of json using C#?
Hello.
There is a simple api , you need to get and display on the form any value (let's say the first one) "idnetrikalpu".
As an example, I tried an article from the forum and other similar articles from the same section.
Habr and Google .
Unfortunately, I could not start parsing.
public class Api
{
public int id { get; set; }
public string idnetrikalpu { get; set; }
public string decription { get; set; }
public string district { get; set; }
public string externallpuid { get; set; }
public bool isactive { get; set; }
public string lpufullname { get; set; }
public string lpushortname { get; set; }
}
public class RootObject
{
public List<Api> api { get; set; }
}
private async Task GetListOfFriends()
{
using (var w = new WebClient())
{
// Скачиваем строку АПИ (начало)
w.Encoding = Encoding.UTF8;
string resp1 = await w.DownloadStringTaskAsync("http://91.237.82.1/api/v1/netrikalpu/");
// Скачиваем строку АПИ (конец)
// Пытаемся привести JSON к канону(начало)
string aaa = resp1.Replace("[", "{\"aaa\":[");
string aaa1 = aaa.Replace("]", "]}");
// Пытаемся привести JSON к канону(конец)
// Парсим (начало)
// Перенести затем внутрь исключения
JObject obj = JObject.Parse(aaa1);
RootObject[] objArr = JsonConvert.DeserializeObject<RootObject[]>(obj["aaa"][0]["lpufullname"].ToString());
// Парсим (конец)
label1.Text = aaa1.ToString().Substring(0, 10);
try
{
//label1.Text = objArr.ToString().Substring(0, 10);
}
catch (Exception e)
{
label1.Text = "1111111";
}
}
}
Answer the question
In order to leave comments, you need to log in
Why bring something? Take jsonutils.com
as an example and feed your json to it.
He says that this is quite valid json and it can be parsed into a list of such classes:
public class Example
{
public int id { get; set; }
public string idnetrikalpu { get; set; }
public string decription { get; set; }
public string district { get; set; }
public string externallpuid { get; set; }
public bool isactive { get; set; }
public string lpufullname { get; set; }
public string lpushortname { get; set; }
}
var fb = new WebClient() {Encoding = Encoding.UTF8}.DownloadString("http://91.237.82.1/api/v1/netrikalpu/?format=json");
var list = JsonConvert.DeserializeObject<List<Example>>(fb);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question