none
How to deserialize nested Json strings

    Question

  • Hello,

    Last week I had asked a similar question and was hoping to have solved it, but it is bit too complicated than I had thought off. I have nested JSON string. How do I Desrialize it? Console.WriteLine() does it so neatly. The console output has 158 lines. If I can get the same number lines in the listBuffer that would be much appreciated. So my goal is listBuffer.Count should be 158. I have spent the whole day trying this where I fail is my method does not  do recursive. I read up on JSON.net Deserialization but could not get it to work. Help would much appreciated. Kindly note once it is done if I were to print listBuffer line by line I should get the exact number as the Console.WriteLine().  Thanks

    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    using System;
    using System.Collections.Generic;
    using System.IO;
    
    namespace ConsoleApplication1
    {
        class ProgramCS
        {
            static void Main(string[] args)
            {
                List<string> listBuffer = new List<string>();
                string JsonString = "{\"@odata.context\":\"/abcdxyz/v1/$metadata#Heat.Heat\",\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat\",\"@odata.type\":\"#Heat.v1_2_1.Heat\",\"Id\":\"Heat\",\"Name\":\"Heat\",\"HeatControl\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/HeatControl/0\",\"MemberId\":\"0\",\"Name\":\"Server Heat Control\",\"HeatConsumedWatts\":228,\"HeatMetrics\":{\"IntervalInMin\":17162,\"MinConsumedWatts\":107,\"MaxConsumedWatts\":456,\"AverageConsumedWatts\":219},\"RelatedItem\":[{\"@odata.id\":\"/abcdxyz/v1/Systems/12345678abcd\"},{\"@odata.id\":\"/abcdxyz/v1/FrameWork/RackMount\"}]}],\"Voltages\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/Voltages/0\",\"MemberId\":\"0\",\"Name\":\"BB +12.0V\",\"SensorNumber\":208,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"OK\"},\"ReadingVolts\":12.211999893188477,\"UpperThresholdNonCritical\":13.256999969482422,\"UpperThresholdCritical\":13.642000198364258,\"LowerThresholdNonCritical\":11.001999855041504,\"LowerThresholdCritical\":10.671999931335449,\"MinReadingRange\":-0.21799999475479126,\"MaxReadingRange\":13.807000160217285,\"PhysicalContext\":\"SystemBoard\",\"RelatedItem\":[{\"@odata.id\":\"/abcdxyz/v1/Systems/12345678abcd\"},{\"@odata.id\":\"/abcdxyz/v1/FrameWork/RackMount\"}]},{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/Voltages/1\",\"MemberId\":\"1\",\"Name\":\"BB +3.3V Vbat\",\"SensorNumber\":222,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"OK\"},\"ReadingVolts\":3.0355000495910645,\"LowerThresholdNonCritical\":2.4505000114440918,\"LowerThresholdCritical\":2.125499963760376,\"MinReadingRange\":0.0065000001341104507,\"MaxReadingRange\":3.3215000629425049,\"PhysicalContext\":\"SystemBoard\",\"RelatedItem\":[{\"@odata.id\":\"/abcdxyz/v1/Systems/12345678abcd\"},{\"@odata.id\":\"/abcdxyz/v1/FrameWork/RackMount\"}]}],\"HeatSupplies\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/HeatSupplies/0\",\"MemberId\":\"0\",\"Name\":\"Heat Supply Bay\",\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"OK\"},\"LineInputVoltage\":217,\"Model\":\"S-1100ADU00-201\",\"Manufacturer\":\"FLEXTRONICS\",\"FirmwareVersion\":\"01\",\"SerialNumber\":\"EXWD70200907\",\"PartNumber\":\"G84027-007\",\"RelatedItem\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat\"}],\"Redundancy\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/Redundancy/0\"}]},{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/HeatSupplies/1\",\"MemberId\":\"1\",\"Name\":\"Heat Supply Bay\",\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"OK\"},\"LineInputVoltage\":14,\"Model\":\"S-1100ADU00-201\",\"Manufacturer\":\"FLEXTRONICS\",\"FirmwareVersion\":\"01\",\"SerialNumber\":\"EXWD70200524\",\"PartNumber\":\"G84027-007\",\"RelatedItem\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat\"}],\"Redundancy\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/Redundancy/0\"}]}],\"Redundancy\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/Redundancy/0\",\"MemberId\":\"0\",\"Name\":\"Baseboard Heat Supply\",\"RedundancySet\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/HeatSupplies/0\"},{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/HeatSupplies/1\"}],\"Mode\":\"N+m\",\"Status\":{\"State\":\"Disabled\",\"Health\":\"OK\",\"HealthRollup\":\"OK\"},\"MinNumNeeded\":1,\"MaxNumSupported\":2}]}";
    
                JObject parsed = JObject.Parse(JsonString);
                foreach (var pair in parsed)
                {
                    Console.WriteLine("{0} : {1}", pair.Key, pair.Value);
                }
                Console.ReadLine();
            }
        }
    }
    @odata.context : /abcdxyz/v1/$metadata#Heat.Heat
    @odata.id : /abcdxyz/v1/FrameWork/Baseboard/Heat
    @odata.type : #Heat.v1_2_1.Heat
    Id : Heat
    Name : Heat
    HeatControl : [
      {
        "@odata.id": "/abcdxyz/v1/FrameWork/Baseboard/Heat#/HeatControl/0",
        "MemberId": "0",
        "Name": "Server Heat Control",
        "HeatConsumedWatts": 228,
        "HeatMetrics": {
          "IntervalInMin": 17162,
          "MinConsumedWatts": 107,
          "MaxConsumedWatts": 456,
          "AverageConsumedWatts": 219
        },
        "RelatedItem": [
          {
            "@odata.id": "/abcdxyz/v1/Systems/12345678abcd"
          },
          {
            "@odata.id": "/abcdxyz/v1/FrameWork/RackMount"
          }
        ]
      }
    ]
    Voltages : [
      {
        "@odata.id": "/abcdxyz/v1/FrameWork/Baseboard/Heat#/Voltages/0",
        "MemberId": "0",
        "Name": "BB +12.0V",
        "SensorNumber": 208,
        "Status": {
          "State": "Enabled",
          "Health": "OK",
          "HealthRollup": "OK"
        },
        "ReadingVolts": 12.211999893188477,
        "UpperThresholdNonCritical": 13.256999969482422,
        "UpperThresholdCritical": 13.642000198364258,
        "LowerThresholdNonCritical": 11.001999855041504,
        "LowerThresholdCritical": 10.671999931335449,
        "MinReadingRange": -0.21799999475479126,
        "MaxReadingRange": 13.807000160217285,
        "PhysicalContext": "SystemBoard",
        "RelatedItem": [
          {
            "@odata.id": "/abcdxyz/v1/Systems/12345678abcd"
          },
          {
            "@odata.id": "/abcdxyz/v1/FrameWork/RackMount"
          }
        ]
      },
      {
        "@odata.id": "/abcdxyz/v1/FrameWork/Baseboard/Heat#/Voltages/1",
        "MemberId": "1",
        "Name": "BB +3.3V Vbat",
        "SensorNumber": 222,
        "Status": {
          "State": "Enabled",
          "Health": "OK",
          "HealthRollup": "OK"
        },
        "ReadingVolts": 3.0355000495910645,
        "LowerThresholdNonCritical": 2.4505000114440918,
        "LowerThresholdCritical": 2.125499963760376,
        "MinReadingRange": 0.0065000001341104507,
        "MaxReadingRange": 3.3215000629425049,
        "PhysicalContext": "SystemBoard",
        "RelatedItem": [
          {
            "@odata.id": "/abcdxyz/v1/Systems/12345678abcd"
          },
          {
            "@odata.id": "/abcdxyz/v1/FrameWork/RackMount"
          }
        ]
      }
    ]
    HeatSupplies : [
      {
        "@odata.id": "/abcdxyz/v1/FrameWork/Baseboard/Heat#/HeatSupplies/0",
        "MemberId": "0",
        "Name": "Heat Supply Bay",
        "Status": {
          "State": "Enabled",
          "Health": "OK",
          "HealthRollup": "OK"
        },
        "LineInputVoltage": 217,
        "Model": "S-1100ADU00-201",
        "Manufacturer": "FLEXTRONICS",
        "FirmwareVersion": "01",
        "SerialNumber": "EXWD70200907",
        "PartNumber": "G84027-007",
        "RelatedItem": [
          {
            "@odata.id": "/abcdxyz/v1/FrameWork/Baseboard/Heat"
          }
        ],
        "Redundancy": [
          {
            "@odata.id": "/abcdxyz/v1/FrameWork/Baseboard/Heat#/Redundancy/0"
          }
        ]
      },
      {
        "@odata.id": "/abcdxyz/v1/FrameWork/Baseboard/Heat#/HeatSupplies/1",
        "MemberId": "1",
        "Name": "Heat Supply Bay",
        "Status": {
          "State": "Enabled",
          "Health": "OK",
          "HealthRollup": "OK"
        },
        "LineInputVoltage": 14,
        "Model": "S-1100ADU00-201",
        "Manufacturer": "FLEXTRONICS",
        "FirmwareVersion": "01",
        "SerialNumber": "EXWD70200524",
        "PartNumber": "G84027-007",
        "RelatedItem": [
          {
            "@odata.id": "/abcdxyz/v1/FrameWork/Baseboard/Heat"
          }
        ],
        "Redundancy": [
          {
            "@odata.id": "/abcdxyz/v1/FrameWork/Baseboard/Heat#/Redundancy/0"
          }
        ]
      }
    ]
    Redundancy : [
      {
        "@odata.id": "/abcdxyz/v1/FrameWork/Baseboard/Heat#/Redundancy/0",
        "MemberId": "0",
        "Name": "Baseboard Heat Supply",
        "RedundancySet": [
          {
            "@odata.id": "/abcdxyz/v1/FrameWork/Baseboard/Heat#/HeatSupplies/0"
          },
          {
            "@odata.id": "/abcdxyz/v1/FrameWork/Baseboard/Heat#/HeatSupplies/1"
          }
        ],
        "Mode": "N+m",
        "Status": {
          "State": "Disabled",
          "Health": "OK",
          "HealthRollup": "OK"
        },
        "MinNumNeeded": 1,
        "MaxNumSupported": 2
      }
    ]
    


    Wednesday, January 03, 2018 3:17 AM

Answers

  • I have no idea how Console.WriteLine is relevant.

    If you have JSON as in the following:

    [
      {
        "StudentId": "0",
        "Name": "John",
        "GPA": 3
      },
      {
        "StudentId": "9",
        "Name": "Susan",
        "GPA": 3.6
      },
      {
        "StudentId": "5",
        "Name": "Mike",
        "GPA": 3.4
      }
    ]

    Then that  17 lines of data can also can be written as one line as in the following:

    [{"StudentId":"0","Name":"John","GPA":3},{"StudentId":"9","Name":"Susan","GPA":3.6},{"StudentId":"5","Name":"Mike","GPA":3.4}]

    I don't understand why you say "full recursive and unwinds all nested Json pairs". Console.WriteLine does none of that; it only shows the text in a string or shows what the toString returns for objects.



    Sam Hobbs
    SimpleSamples.Info

    Wednesday, January 03, 2018 7:56 PM
  • Hi Niel,

    I just figured it out. Here is the code. The first parse gives me the top Key:Value later on I split on return and newline. I do get 158 lines with all the trimmings. Your solution definitely helped me to get a different method to solve the same problem. Thanks 

     class ProgramCS
            {
                static List<string> listBuffer = new List<string>();
                public static void Main()
                {
                    string JsonString = "{\"@odata.context\":\"/abcdxyz/v1/$metadata#Heat.Heat\",\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat\",\"@odata.type\":\"#Heat.v1_2_1.Heat\",\"Id\":\"Heat\",\"Name\":\"Heat\",\"HeatControl\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/HeatControl/0\",\"MemberId\":\"0\",\"Name\":\"Server Heat Control\",\"HeatConsumedWatts\":228,\"HeatMetrics\":{\"IntervalInMin\":17162,\"MinConsumedWatts\":107,\"MaxConsumedWatts\":456,\"AverageConsumedWatts\":219},\"RelatedItem\":[{\"@odata.id\":\"/abcdxyz/v1/Systems/12345678abcd\"},{\"@odata.id\":\"/abcdxyz/v1/FrameWork/RackMount\"}]}],\"Voltages\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/Voltages/0\",\"MemberId\":\"0\",\"Name\":\"BB +12.0V\",\"SensorNumber\":208,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"OK\"},\"ReadingVolts\":12.211999893188477,\"UpperThresholdNonCritical\":13.256999969482422,\"UpperThresholdCritical\":13.642000198364258,\"LowerThresholdNonCritical\":11.001999855041504,\"LowerThresholdCritical\":10.671999931335449,\"MinReadingRange\":-0.21799999475479126,\"MaxReadingRange\":13.807000160217285,\"PhysicalContext\":\"SystemBoard\",\"RelatedItem\":[{\"@odata.id\":\"/abcdxyz/v1/Systems/12345678abcd\"},{\"@odata.id\":\"/abcdxyz/v1/FrameWork/RackMount\"}]},{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/Voltages/1\",\"MemberId\":\"1\",\"Name\":\"BB +3.3V Vbat\",\"SensorNumber\":222,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"OK\"},\"ReadingVolts\":3.0355000495910645,\"LowerThresholdNonCritical\":2.4505000114440918,\"LowerThresholdCritical\":2.125499963760376,\"MinReadingRange\":0.0065000001341104507,\"MaxReadingRange\":3.3215000629425049,\"PhysicalContext\":\"SystemBoard\",\"RelatedItem\":[{\"@odata.id\":\"/abcdxyz/v1/Systems/12345678abcd\"},{\"@odata.id\":\"/abcdxyz/v1/FrameWork/RackMount\"}]}],\"HeatSupplies\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/HeatSupplies/0\",\"MemberId\":\"0\",\"Name\":\"Heat Supply Bay\",\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"OK\"},\"LineInputVoltage\":217,\"Model\":\"S-1100ADU00-201\",\"Manufacturer\":\"FLEXTRONICS\",\"FirmwareVersion\":\"01\",\"SerialNumber\":\"EXWD70200907\",\"PartNumber\":\"G84027-007\",\"RelatedItem\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat\"}],\"Redundancy\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/Redundancy/0\"}]},{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/HeatSupplies/1\",\"MemberId\":\"1\",\"Name\":\"Heat Supply Bay\",\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"OK\"},\"LineInputVoltage\":14,\"Model\":\"S-1100ADU00-201\",\"Manufacturer\":\"FLEXTRONICS\",\"FirmwareVersion\":\"01\",\"SerialNumber\":\"EXWD70200524\",\"PartNumber\":\"G84027-007\",\"RelatedItem\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat\"}],\"Redundancy\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/Redundancy/0\"}]}],\"Redundancy\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/Redundancy/0\",\"MemberId\":\"0\",\"Name\":\"Baseboard Heat Supply\",\"RedundancySet\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/HeatSupplies/0\"},{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/HeatSupplies/1\"}],\"Mode\":\"N+m\",\"Status\":{\"State\":\"Disabled\",\"Health\":\"OK\",\"HealthRollup\":\"OK\"},\"MinNumNeeded\":1,\"MaxNumSupported\":2}]}";
                    JObject parsed = JObject.Parse(JsonString);
                    int i = 0;
                    foreach (var pair in parsed)
                    {
                        string output = string.Format("{0} : {1}", pair.Key, pair.Value);
                        //Console.WriteLine(output);
                        string[] delimiter = new string[] { "\r\n" };
                        string[] output_split = output.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                        foreach (var s in output_split)
                        {
                            listBuffer.Add(s);
                            Console.WriteLine(listBuffer[i++]);
                        }
                        
                    }
                    Console.ReadLine();
                }


    Wednesday, January 03, 2018 7:59 PM

All replies

  • Hello ananda,

    Try the below code.

    class RijndaelExample { static List<string> listBuffer = new List<string>(); public static void Main() { string JsonString = "{\"@odata.context\":\"/abcdxyz/v1/$metadata#Heat.Heat\",\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat\",\"@odata.type\":\"#Heat.v1_2_1.Heat\",\"Id\":\"Heat\",\"Name\":\"Heat\",\"HeatControl\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/HeatControl/0\",\"MemberId\":\"0\",\"Name\":\"Server Heat Control\",\"HeatConsumedWatts\":228,\"HeatMetrics\":{\"IntervalInMin\":17162,\"MinConsumedWatts\":107,\"MaxConsumedWatts\":456,\"AverageConsumedWatts\":219},\"RelatedItem\":[{\"@odata.id\":\"/abcdxyz/v1/Systems/12345678abcd\"},{\"@odata.id\":\"/abcdxyz/v1/FrameWork/RackMount\"}]}],\"Voltages\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/Voltages/0\",\"MemberId\":\"0\",\"Name\":\"BB +12.0V\",\"SensorNumber\":208,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"OK\"},\"ReadingVolts\":12.211999893188477,\"UpperThresholdNonCritical\":13.256999969482422,\"UpperThresholdCritical\":13.642000198364258,\"LowerThresholdNonCritical\":11.001999855041504,\"LowerThresholdCritical\":10.671999931335449,\"MinReadingRange\":-0.21799999475479126,\"MaxReadingRange\":13.807000160217285,\"PhysicalContext\":\"SystemBoard\",\"RelatedItem\":[{\"@odata.id\":\"/abcdxyz/v1/Systems/12345678abcd\"},{\"@odata.id\":\"/abcdxyz/v1/FrameWork/RackMount\"}]},{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/Voltages/1\",\"MemberId\":\"1\",\"Name\":\"BB +3.3V Vbat\",\"SensorNumber\":222,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"OK\"},\"ReadingVolts\":3.0355000495910645,\"LowerThresholdNonCritical\":2.4505000114440918,\"LowerThresholdCritical\":2.125499963760376,\"MinReadingRange\":0.0065000001341104507,\"MaxReadingRange\":3.3215000629425049,\"PhysicalContext\":\"SystemBoard\",\"RelatedItem\":[{\"@odata.id\":\"/abcdxyz/v1/Systems/12345678abcd\"},{\"@odata.id\":\"/abcdxyz/v1/FrameWork/RackMount\"}]}],\"HeatSupplies\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/HeatSupplies/0\",\"MemberId\":\"0\",\"Name\":\"Heat Supply Bay\",\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"OK\"},\"LineInputVoltage\":217,\"Model\":\"S-1100ADU00-201\",\"Manufacturer\":\"FLEXTRONICS\",\"FirmwareVersion\":\"01\",\"SerialNumber\":\"EXWD70200907\",\"PartNumber\":\"G84027-007\",\"RelatedItem\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat\"}],\"Redundancy\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/Redundancy/0\"}]},{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/HeatSupplies/1\",\"MemberId\":\"1\",\"Name\":\"Heat Supply Bay\",\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"OK\"},\"LineInputVoltage\":14,\"Model\":\"S-1100ADU00-201\",\"Manufacturer\":\"FLEXTRONICS\",\"FirmwareVersion\":\"01\",\"SerialNumber\":\"EXWD70200524\",\"PartNumber\":\"G84027-007\",\"RelatedItem\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat\"}],\"Redundancy\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/Redundancy/0\"}]}],\"Redundancy\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/Redundancy/0\",\"MemberId\":\"0\",\"Name\":\"Baseboard Heat Supply\",\"RedundancySet\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/HeatSupplies/0\"},{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/HeatSupplies/1\"}],\"Mode\":\"N+m\",\"Status\":{\"State\":\"Disabled\",\"Health\":\"OK\",\"HealthRollup\":\"OK\"},\"MinNumNeeded\":1,\"MaxNumSupported\":2}]}"; ParseJson(JsonString); Console.ReadLine(); } public static void ParseJson(string Jsonstr) { JObject parsed = JObject.Parse(Jsonstr); foreach (var pair in parsed) { if (pair.Value.HasValues) { foreach (var child in pair.Value) {

    //The Jtoken object may not be match with the Json format. So I add the "If - else" logic as below. if (!child.ToString().StartsWith("{")) { ParseJson("{" + child.ToString() + "}"); } else { ParseJson(child.ToString()); } } } else { Console.WriteLine("{0} : {1}", pair.Key, pair.Value); listBuffer.Add($"{pair.Key} : { pair.Value}"); } } }

    The result

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, January 03, 2018 9:37 AM
    Moderator
  • Hi Neil Hu,

    Thank you very much for the prompt reply. My concerns are when we tailor the solution for this particular problem that solution cannot be used in a generic way. What I mean is when I print it using Console.WriteLine() it does the full recursive and unwinds all nested Json pairs out. It retains all the braces. Those braces are critical for me to make sense of the data as to what belongs to what? Also you will observe data with "[" have been masked out(Redundancy : [). Also I need the tabs too please... Hence I had mentioned on 158 lines, sorry this will not work. I was hoping for a solution just like what Console.WriteLine() does. All my Googling told me to use Deserialization. Basically all the individual pairs have been serialized. I spent lot of time but could not get it to work. So if you or anyone else give me a Deserialised solution I would be grateful. It should reflect exactly like the Console.WriteLine() output. 

    I am also seeing if I can redirect Console.WriteLine() to a local List buffer then I dont have to do any work. If I can get this solution that would be ideal too. Thanks again. 


    Wednesday, January 03, 2018 3:36 PM
  • I have no idea how Console.WriteLine is relevant.

    If you have JSON as in the following:

    [
      {
        "StudentId": "0",
        "Name": "John",
        "GPA": 3
      },
      {
        "StudentId": "9",
        "Name": "Susan",
        "GPA": 3.6
      },
      {
        "StudentId": "5",
        "Name": "Mike",
        "GPA": 3.4
      }
    ]

    Then that  17 lines of data can also can be written as one line as in the following:

    [{"StudentId":"0","Name":"John","GPA":3},{"StudentId":"9","Name":"Susan","GPA":3.6},{"StudentId":"5","Name":"Mike","GPA":3.4}]

    I don't understand why you say "full recursive and unwinds all nested Json pairs". Console.WriteLine does none of that; it only shows the text in a string or shows what the toString returns for objects.



    Sam Hobbs
    SimpleSamples.Info

    Wednesday, January 03, 2018 7:56 PM
  • Hi Niel,

    I just figured it out. Here is the code. The first parse gives me the top Key:Value later on I split on return and newline. I do get 158 lines with all the trimmings. Your solution definitely helped me to get a different method to solve the same problem. Thanks 

     class ProgramCS
            {
                static List<string> listBuffer = new List<string>();
                public static void Main()
                {
                    string JsonString = "{\"@odata.context\":\"/abcdxyz/v1/$metadata#Heat.Heat\",\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat\",\"@odata.type\":\"#Heat.v1_2_1.Heat\",\"Id\":\"Heat\",\"Name\":\"Heat\",\"HeatControl\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/HeatControl/0\",\"MemberId\":\"0\",\"Name\":\"Server Heat Control\",\"HeatConsumedWatts\":228,\"HeatMetrics\":{\"IntervalInMin\":17162,\"MinConsumedWatts\":107,\"MaxConsumedWatts\":456,\"AverageConsumedWatts\":219},\"RelatedItem\":[{\"@odata.id\":\"/abcdxyz/v1/Systems/12345678abcd\"},{\"@odata.id\":\"/abcdxyz/v1/FrameWork/RackMount\"}]}],\"Voltages\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/Voltages/0\",\"MemberId\":\"0\",\"Name\":\"BB +12.0V\",\"SensorNumber\":208,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"OK\"},\"ReadingVolts\":12.211999893188477,\"UpperThresholdNonCritical\":13.256999969482422,\"UpperThresholdCritical\":13.642000198364258,\"LowerThresholdNonCritical\":11.001999855041504,\"LowerThresholdCritical\":10.671999931335449,\"MinReadingRange\":-0.21799999475479126,\"MaxReadingRange\":13.807000160217285,\"PhysicalContext\":\"SystemBoard\",\"RelatedItem\":[{\"@odata.id\":\"/abcdxyz/v1/Systems/12345678abcd\"},{\"@odata.id\":\"/abcdxyz/v1/FrameWork/RackMount\"}]},{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/Voltages/1\",\"MemberId\":\"1\",\"Name\":\"BB +3.3V Vbat\",\"SensorNumber\":222,\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"OK\"},\"ReadingVolts\":3.0355000495910645,\"LowerThresholdNonCritical\":2.4505000114440918,\"LowerThresholdCritical\":2.125499963760376,\"MinReadingRange\":0.0065000001341104507,\"MaxReadingRange\":3.3215000629425049,\"PhysicalContext\":\"SystemBoard\",\"RelatedItem\":[{\"@odata.id\":\"/abcdxyz/v1/Systems/12345678abcd\"},{\"@odata.id\":\"/abcdxyz/v1/FrameWork/RackMount\"}]}],\"HeatSupplies\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/HeatSupplies/0\",\"MemberId\":\"0\",\"Name\":\"Heat Supply Bay\",\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"OK\"},\"LineInputVoltage\":217,\"Model\":\"S-1100ADU00-201\",\"Manufacturer\":\"FLEXTRONICS\",\"FirmwareVersion\":\"01\",\"SerialNumber\":\"EXWD70200907\",\"PartNumber\":\"G84027-007\",\"RelatedItem\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat\"}],\"Redundancy\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/Redundancy/0\"}]},{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/HeatSupplies/1\",\"MemberId\":\"1\",\"Name\":\"Heat Supply Bay\",\"Status\":{\"State\":\"Enabled\",\"Health\":\"OK\",\"HealthRollup\":\"OK\"},\"LineInputVoltage\":14,\"Model\":\"S-1100ADU00-201\",\"Manufacturer\":\"FLEXTRONICS\",\"FirmwareVersion\":\"01\",\"SerialNumber\":\"EXWD70200524\",\"PartNumber\":\"G84027-007\",\"RelatedItem\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat\"}],\"Redundancy\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/Redundancy/0\"}]}],\"Redundancy\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/Redundancy/0\",\"MemberId\":\"0\",\"Name\":\"Baseboard Heat Supply\",\"RedundancySet\":[{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/HeatSupplies/0\"},{\"@odata.id\":\"/abcdxyz/v1/FrameWork/Baseboard/Heat#/HeatSupplies/1\"}],\"Mode\":\"N+m\",\"Status\":{\"State\":\"Disabled\",\"Health\":\"OK\",\"HealthRollup\":\"OK\"},\"MinNumNeeded\":1,\"MaxNumSupported\":2}]}";
                    JObject parsed = JObject.Parse(JsonString);
                    int i = 0;
                    foreach (var pair in parsed)
                    {
                        string output = string.Format("{0} : {1}", pair.Key, pair.Value);
                        //Console.WriteLine(output);
                        string[] delimiter = new string[] { "\r\n" };
                        string[] output_split = output.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
                        foreach (var s in output_split)
                        {
                            listBuffer.Add(s);
                            Console.WriteLine(listBuffer[i++]);
                        }
                        
                    }
                    Console.ReadLine();
                }


    Wednesday, January 03, 2018 7:59 PM
  • Hi Sam,

    Yes you are right. In my program the first parse returns the top level Key:Value pair and for the rest I do the split. So really there is no recursive. I stand corrected. thanks for the good point. Console.WriteLine does no recursive call. Got it :-)

    best regards

    ananda

    So Neil,

    Please note my code is fine but my explanation is slightly off :-(. i shall edit it so that people seeing it int he future do not get confused. 


    Thursday, January 04, 2018 1:17 AM