There is a response from the server in the form of json:

{ "14.05.2018": { "Min": "2", "Max": "7" }, "15.05.2018": { "Min": "1", "Max": "7" } } 

It is necessary in C # on mind. I understand this is a two-dimensional associative array.

Tell me how to be. The newcomer himself in this case has not worked with json before.

  • In C #, there is a Dictionary for this - Andrew NOP
  • 2
    In my opinion, this is the most typical json question, it has already been answered several times. It is time to write a reference closet and make a FAQ. - AK
  • one
    You use for example the newtonsoft library. JsonConvert.DeserializeObject<Dictionary<string, Data>>(json) . Where Data class with fields max, min. - adrug
  • Duplicate? ru.stackoverflow.com/q/829577/218063 - Andrew NOP
  • one
    Probably there is no point in copying your own answers from other similar questions: D - adrug

3 answers 3

Training

Let's see what your JSON is. I personally use this resource for such purposes. We insert our data there and see the following picture:

Jsonview

What we have here?

  • We have the first level (let's call it Root).
    • Root contains some objects that have a name and something inside (let's call internal objects, for example, Data).
      • Inside Data we see two values ​​(min and max).

Well, we figured out the structure, then we will need something that will make life easier for us when working with JSON format, I personally recommend using Newtonsoft.Json , an excellent library for working with JSON!

Deserialization

Having dealt with the structure and setting the necessary, you can proceed to deserialization (JSON conversion into an object).

  • Let's go from the very end, namely, let's create Data for the beginning. We remember that Data has 2 values ​​of min and max, both contain a number (that is, int). Having all this, we can write the following class:

     public class Data { public int Min { get; set; } public int Max { get; set; } } 

Note that all variables here are written according to the CamelCase rules, that is, with a capital letter. I also advise you to stick to this style when writing such classes, but you need to remember: you can change the register in the name without problems, but if the name in the class differs from that in JSON, we won’t get the data and therefore you should mark such renames with the attribute [JsonProperty("oldName")] with the name that is in JSON.

  • So, having a class Data, we can go a higher level, create an object with the name and Data inside. For this, in C # it is customary to use a Dictionary (dictionary), where Key is the name (in your case, date), and Value is data (in your case, the Data class). Based on this, we can write the following:

     var source = "{\"14.05.2018\":{\"min\":\"2\",\"max\":\"7\"},\"15.05.2018\":{\"min\":\"1\",\"max\":\"7\"}}"; var parsed = JsonConvert.DeserializeObject<Dictionary<string, Data>>(source); 

Let me explain, we call the DeserializeObject method, which we set the data type to Dictionary<string, Data> , and we pass the source - our JSON in the form of a string.

Actually and everything, with the result we get a Dictionary<string, Data> object Dictionary<string, Data> from which we can get what we need, for example like this:

 parsed["14.05.2018"].Max; 

Or go through the cycle:

 foreach (var data in parsed) { Console.WriteLine($"Key: {data.Key}"); Console.WriteLine($"Max: {data.Value.Max} Min: {data.Value.Min}"); } 

The parsed object itself will have approximately the following structure:

Result

By the way! To work with JSON, resources that help you create the necessary class structure, for example this one , are very cool. By inserting your JSON in the left field and specifying the desired class name ( Data ) in the Name field, the site will give us the following structure:

 public partial class Data { [JsonProperty("min")] public string Min { get; set; } [JsonProperty("max")] public string Max { get; set; } } public partial class Data { public static Dictionary<string, Data> FromJson(string json) => JsonConvert.DeserializeObject<Dictionary<string, Data>>(json, QuickType.Converter.Settings); } 

Do not like anything? In general, use the thing is very useful!
I hope helped. Good luck learning C #!

  • 2
    The studio has a built-in tool for translating json into classes: Edit -> Paste special -> Paste JSON as classes - Grundy
  • 3
    @Grundy I do not find this functionality good. For example, JSON specifying json from the question - issued it - EvgeniyZ
  • Thanks for the educational program! very interesting!!! - 404

As you have already been suggested in the comments to the question, if you need an associative array, use it. The most popular serializer - JSON.NET - fully supports it.

You need to get a Dictionary<string, Range> , where Range :

 // переименовать по желанию public class Range { public string Min { get; set; } public string Max { get; set; } } 

Deserialization:

 var deserialized = JsonConvert.DeserializeObject<Dictionary<string, Range>>(jsonString); 
  • Potential minus - please justify the minuses :) - PashaPash

I would venture to suggest a handwritten version of the converter, they are not very difficult to customize for their tasks, here is an example of such a converter with the ability to set the json source format using attributes . I would be glad if my answer will help you choose the best solution.