S
S
Sergey Karbivnichy2015-02-05 23:15:43
JSON
Sergey Karbivnichy, 2015-02-05 23:15:43

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:
15b37b8cfca24a66954e6893e617d296.jpg
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

  }
  ]
  }
}

PS: Of course, you can make it on delphi dll, which will process json, then connect this dll to a project in C #, but this is at worst, of course, since I do not consider this method correct.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2015-02-06
@hottabxp

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.

O
Oxoron, 2015-02-06
@Oxoron

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;
        }
    }

This class serializes/deserializes objects.
public class Item 
{
...
public string title{get; set;}
public string url{get;set;}
...
}

This is a single entry class. For serialization, use auto-properties (the same get; set;), be sure to public.
public class Response
{
public int count{get;set;}
public List<Item> items{get; set;}
}

To get data use code like
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(); // Массив адресов

library: newtonsoft.json.dll
If necessary, I can throw off a ready-made example (is there a personal on the toaster?).
Also visit json2csharp.com
Paste your sample Json into the field, get the required classes right away.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question