-2

I have a string dictionary as shown in below. As you see, keys are string dates, but their values are dictionary.

"{"19-02-2020":{"1":"ABC","2":"DEF"},"20-02-2020":{"1" : "GHI","2" : "JKL","3" : "MNO"},"21-02-2020" : {"1" : "PRS"}}"

That can be written to be more understandable;

"{
  "19-02-2020" : {"1" : "ABC", "2" : "DEF"},
  "20-02-2020" : {"1" : "GHI", "2" : "JKL", "3" : "MNO"},
  "21-02-2020" : {"1" : "PRS"}
 }"

How can I get the string dates in string format? I tried to split this string with "," or something more. But it seems impossible because I will get more dates with its dictionary values in the future. Is there any way to get keys from string dictionary?

I want to get an array which contains string dates like ["19-02-2020","20-02-2020","21-02-2020"]

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
Wicaledon
  • 710
  • 1
  • 11
  • 26
  • 5
    It would be easier to help you if you could provide a [mcve] with the expected output. Also, this looks like JSON - any reason you're not just parsing it *as* JSON, e.g. using Json.NET to create a JObject? – Jon Skeet Feb 21 '20 at 08:15
  • 1
    @JonSkeet Thanks for warning. I added my expected output. Yes it looks like json but I have to use this with string format. – Wicaledon Feb 21 '20 at 08:19
  • 1
    That's still not a [mcve]. *Please* provide one - code we can copy, paste, compile. At the moment it's still unclear what you've really got. – Jon Skeet Feb 21 '20 at 08:22
  • 2
    dictionary has a `Keys` property, you can use it – Pavel Anikhouski Feb 21 '20 at 08:23
  • Just to get this right, you need all key values from your `Dictionaty>` and compile them all into a `String[]` array? – Simon Jensen Feb 21 '20 at 08:24
  • 1
    @Wicaledon, do you really have a `string dictionary`, or actually `a string` with that json content? Is the real question "how to parse JSON"? – Andrew Feb 21 '20 at 08:24
  • @Andrew you can think like I have `string mydict = "{"19-02-2020":{"1":"ABC","2":"DEF"},"20-02-2020":{"1" : "GHI","2" : "JKL","3" : "MNO"},"21-02-2020" : {"1" : "PRS"}}"` – Wicaledon Feb 21 '20 at 08:25
  • Then I guess you need to take a look at [this other question](https://stackoverflow.com/questions/6620165/how-can-i-parse-json-with-c). – Andrew Feb 21 '20 at 08:27

2 Answers2

2

You can parse your JSON string into JObject from Newtonsoft.Json.Linq namespace. It implements IDictionary<string, JToken>, so you easily get the key as string

var jObject = JObject.Parse(json);

foreach (var item in jObject) 
    Console.WriteLine(item.Key);

The output will be the following

enter image description here

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
0

As your data is just a dictionary with a string key and dictionary value, you can easily deserialize it (e.g. with Newtonsoft.Json) and extract the values you need.

using Newtonsoft.Json;
var deserializedDictionary = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, string>>>(yourString);

// The data you are interested in.
var dates = dict.Keys.ToArray();
mcjmzn
  • 353
  • 6
  • 13
  • thanks for answer but for example how can I get the first date? I thought `dates` 's type is array so I tried dates[0] but didn't work – Wicaledon Feb 21 '20 at 08:42
  • I made an edit. Just add ```.ToArray()``` to make the keys colleciton an array. – mcjmzn Feb 21 '20 at 08:47