Untitled

mail@pastecode.io avatar
unknown
csharp
3 years ago
1.6 kB
2
Indexable
Never
void Main()
{
    string json = @"[[
      {
        ""M1"": ""True"",
        ""A1"": ""Este es un campo feliz"",
        ""M2"": ""False"",
        ""A2"": ""Tormsy""
      }
    ],
    [
      {
        ""M1"": ""True"",
        ""A1"": ""Este es un campo feliz"",
        ""M2"": ""False"",
        ""A2"": ""Tormsy""
      },
    ],
    [
     {
       ""M1"": ""True"",
       ""A1"": ""Este es un campo feliz"",
       ""M2"": ""False"",
       ""A2"": ""Tormsy""
     },
     {
       ""MX"": ""True"",
       ""AX"": ""Variable Text"",
       ""MY"": ""False"",
       ""AY"": ""Variable Text""
     }
    ]]";
    
    var array = JArray.Parse(json);
    List<KeyValuePair<string, string>> tokens = new ();
    GetAllTokens(array, tokens);
    var groupedByIndex = 
        from token in tokens
        group token by token.Key[1..];
    
    foreach (var grp in groupedByIndex)
    {
        grp.Key.Dump();
        var groupedByChar = 
            from g in grp
            group g by g.Key[0];
        groupedByChar.Dump();
    }
}

// You can define other methods, fields, classes and namespaces here
public void GetAllTokens(JArray array, List<KeyValuePair<string, string>> tokens)
{
    foreach (var item in array)
    {
        if (item is JArray subArray)
        {
            GetAllTokens(subArray, tokens);
        }
        else if (item is JObject obj)
        {
            foreach (var token in obj)
            {
                tokens.Add(new KeyValuePair<string, string>(token.Key, token.Value.ToString()));
            }
        }
    }
}