Request JSON arrays with Linq, JSON.NET, C #

This post is an attempt to ask a more direct question related to my other recent publication ( Selecting Simple Properties from Hierarchical JSON Part II ):

The specified JSON sub-document as such:

{
  "Array1": {
    "Array1A": [
      { "Item1": "1" },
      { "Item2": "2" },
      { "Item3": "3" }
    ],
    "Array1B": [
      { "Item1": "1" },
      { "Item2": "2" },
      { "Item3": "3" }
    ]
  },
  "Array2": {
    "Array2A": [
      { "Item1": "1" },
      { "Item2": "2" },
      { "Item3": "3" }
    ]
  },
  "Array3": {
    "Array3A": [
      { "Item1": "1" },
      { "Item2": "2" },
      { "Item3": "3" }
    ],
    "Array3B": [
      { "Item1": "1" },
      { "Item2": "2" },
      {
        "Array3B1": [
          { "Item1": "1" },
          { "Item2": "2" },
          { "Item3": "3" }
        ]
      }
    ],
    "Array3C": [
      { "Item1": "1" },
      { "Item2": "2" },
      { "Item3": "3" }
    ]
  }
}

(Note: The above has been confirmed using JSLint.)

Please note that JSON is dynamic - I don’t know in advance how many arrays will be or how deep the arrays will be.

: - ( Array1, Array2, Array3, Array3A, Array3B Array3B1) List<JObject>. JProperty, . List , List<JObject>, . , Array1 ; Array2 - Array1, Array3 - Array2, Array3A - Array3, Array3B - Array3, Array 3B1 - Array3B...

: 1. # Linq List<JObject>, :

list[0]: 
{"Name":"Array1","Parent":""}

list[1]:
{"Name":"Array1A","Item1":"1","Item2":"2","Item3":"3","Parent":"Array1"}

list[2]:
{"Name":"Array1B","Item1":"1","Item2":"2","Item3":"3","Parent":"Array1"}

list[3]:
{"Name":"Array2","Parent":""}

list[4]:
{"Name":"Array2A","Item1":"1","Item2":"2","Item3":"3","Parent":"Array2"}

list[5]:
{"Name":"Array3","Parent":""}

list[6]:
{"Name":"Array3A","Item1":"1","Item2":"2","Item3":"3","Parent":"Array3"}

list[7]:
{"Name":"Array3B","Item1":"1","Item2":"2","Parent":"Array3"}

list[8]:
{"Name":"Array3B1","Item1":"1","Item2":"2","Item3":"3","Parent":"ArrayB"}

list[9]:
{"Name":"Array3C","Item1":"1","Item2":"2","Item3":"3","Parent":"Array3"}

:

  • List<JObject> .
  • [7] JSON Item2, . [8] .
+2
1

- :

List<JObject> list = 
    JObject.Parse(json)
           .Descendants()
           .Where(jt => jt.Type == JTokenType.Property && ((JProperty)jt).Value.HasValues)
           .Cast<JProperty>()
           .Select(prop =>
           {
               var obj = new JObject(new JProperty("Name", prop.Name));
               if (prop.Value.Type == JTokenType.Array)
               {
                   var items = prop.Value.Children<JObject>()
                                         .SelectMany(jo => jo.Properties())
                                         .Where(jp => jp.Value.Type == JTokenType.String);
                   obj.Add(items);
               }
               var parentName = prop.Ancestors()
                                    .Where(jt => jt.Type == JTokenType.Property)
                                    .Select(jt => ((JProperty)jt).Name)
                                    .FirstOrDefault();
               obj.Add("Parent", parentName ?? "");
               return obj;
           })
           .ToList();

Fiddle: https://dotnetfiddle.net/FMxzls

LINQ-to-JSON, :

  • json JObject

    JObject.Parse(json)
    
  • JObject, JTokens

           .Descendants()
    
  • JProperties,

           .Where(jt => jt.Type == JTokenType.Property && ((JProperty)jt).Value.HasValues)
    
  • JTokens JProperties, .

           .Cast<JProperty>()
    
  • , JProperty, :

           .Select(prop =>
           {
    
  • JObject JProperty Name

               var obj = new JObject(new JProperty("Name", prop.Name));
    
  • JProperty ...

               if (prop.Value.Type == JTokenType.Array)
               {
    
  • , JObjects

                   var items = prop.Value.Children<JObject>()
    
  • JObjects JProperties

                                         .SelectMany(jo => jo.Properties())
    
  • JProperties, , )

                                         .Where(jp => jp.Value.Type == JTokenType.String);
    
  • JProperties JObject,

                    obj.Add(items);
                }
    
  • JProperty JProperty

                var parentName = prop.Ancestors()
                                     .Where(jt => jt.Type == JTokenType.Property)
                                     .Select(jt => ((JProperty)jt).Name)
                                     .FirstOrDefault();
    
  • JObject, ; ,

                obj.Add("Parent", parentName ?? "");
    
  •             return obj;
            })
    
  • , JObjects, .

            .ToList();
    
+2

Source: https://habr.com/ru/post/1648875/


All Articles