Answer the question
In order to leave comments, you need to log in
Who can give an example of json parsing in C#?
I have been struggling in c # with json for several days now. Working with json or xml in delphi is very easy compared to C#. I rummaged through about a hundred articles and forums on working with json in C #, but they contain something like this:
This picture also applies to the documentation of libraries for working with json.
Who can provide minimal C# code? You need to enter values with title and url in the ListBox.
{
"response":{
"count":198,
"items":[
{
"id":340125039,
"owner_id":34254853,
"artist":"IOWA",
"title":"Маршрутка",
"duration":190,
"url":"http:\/\/cs7-5v4.vk-cdn.net\/p12\/630de313d73392.mp3?extra=fNremCtKgl4pnCbyZ0q-LOBIfvRoUf5QGo8oWwYrOCza6AlSUOdYIImUideFd3hpPa325ckSD_c3zQ5FNluMS80lrSJiM6FC",
"genre_id":9
},
{
"id":340120602,
"owner_id":34254853,
"artist":"Stromae",
"title":"Tous Les Memes",
"duration":168,
"url":"http:\/\/cs7-1v4.vk-cdn.net\/p18\/600b1a66ef64d6.mp3?extra=GNP79R4nHR39dY50YJ_ddDxEuX6SQcxtG3nCgzI0LdvxoaLh5lQ2qZqqpABCqPoXOISyNzWOqMZx4_nTL3bQ31i57z-UXl4V",
"lyrics_id":99395752
}
]
}
}
Answer the question
In order to leave comments, you need to log in
Here is an example very similar to yours:
stackoverflow.com/questions/13469666/how-to-parse-...
Used by LitJson. Essence: start classes for serialization, in them you do get-ers on fields named the same as json fields. All. In the example, the link even has an array.
Slightly more difficult - JBoy. You can also use it, the example is also easy to google.
internal static class Serializer
{
internal static void Serialize<T>(this T arg, string fileName)
{
string res = JsonConvert.SerializeObject(arg, Formatting.Indented);
File.WriteAllText(fileName,res);
}
internal static T Deserialize<T>(string fileName)
{
string json = File.ReadAllText(fileName);
T res = JsonConvert.DeserializeObject<T>(json);
return res;
}
}
public class Item
{
...
public string title{get; set;}
public string url{get;set;}
...
}
public class Response
{
public int count{get;set;}
public List<Item> items{get; set;}
}
string pathToFIle = @"C:\temp\anyFile";
Response response = Serializer.Deserialize<Response>(pathToFile);
String[] titles = response.items.Select(item => item.title).ToArray(); // Массив названий
String[] urls = response.items.Select(item => item.url)ToArray(); // Массив адресов
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question