1

is there a way to convert JSON file into a C# list without using a helper class ?

i have read some articl about using Dynamics, but i don't know how to loop through items inside dynamic object.

here what i have found so far :

StreamReader r = new StreamReader(@"C:\Users\barras\Desktop\P922 test File\Itacc_Files\OpenNet_P922x_NM_31698.json");
  string jsonString = r.ReadToEnd();
            dynamic  JsonDyn= Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString); 

now, i need to loop through each item in dynamic object JsonDyn and fill the data into a list.

here Json File structure :

{
    "OpenNet_PoGnd":
        {
            "Pins": ["CBGND_1","CBD_7","CT2_4","CBD_6" ]
        },
    "OpenNet_L36":
        {
            "Pins": ["CBF_22","CBF_9"]
        },
    "OpenNet_L37":
        {
            "Pins": ["CT2_1","CBF_20","CT1_2","CBF_18"]
        },
    "OpenNet_IC104":
        {
            "Pins": ["CN5_4","CBC_40"]
        },
......
}
Andy
  • 49,085
  • 60
  • 166
  • 233
  • What do you mean by "without using a helper class"? Do you mean "without using a strongly-typed deserialization-target object"? – David L Jan 27 '15 at 22:03
  • is the "getdynamicmembernames" ( see https://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.getdynamicmembernames(v=vs.110).aspx) functionality of the dynamic object what you are looknig for? – Marvin Smit Jan 27 '15 at 22:05
  • With a little bit of google and a whole lot of lingo, magic happens. Many similar questions like this one have been asked before. Have a look at the following: http://stackoverflow.com/questions/13297563/read-and-parse-a-json-file-in-c-sharp – James Shaw Jan 27 '15 at 22:08
  • Here is another example: http://stackoverflow.com/questions/3973878/load-json-data-stream-from-text-file-into-objects-c-sharp – James Shaw Jan 27 '15 at 22:09
  • And one last one, I promise. :) http://stackoverflow.com/questions/2246694/how-to-convert-json-object-to-custom-c-sharp-object – James Shaw Jan 27 '15 at 22:12
  • @James Shaw thanks for the reply ^^, but all these examples use a class where you define members constituing Json file, my goal is to rea and parse jason file without using a helper class – Badre Arras Jan 27 '15 at 22:21
  • 1
    Not a problem. Perhaps if you expand on what you are trying to accomplish and why a helper class isn't considered a viable solution? Then we can gear our answers around that. – James Shaw Jan 27 '15 at 22:23
  • 1
    Can you parse the json to a dictionary for example? Then you can access via Key/Value pairs. – James Shaw Jan 27 '15 at 22:26
  • i have a file that is feeded automatically each 1min by different item,structure of the file is like so : "OpenNet_PoGnd": { "Pins": ["CBGND_1","CBD_7","CT2_4","CBD_6" ] }, "OpenNet_L36": { "Pins": ["CBF_22","CBF_9"] },..... – Badre Arras Jan 27 '15 at 22:27
  • 1
    Would this be more what you are looking for? http://stackoverflow.com/questions/13683757/newtonsoft-json-dynamic-objects – James Shaw Jan 27 '15 at 22:28
  • and i want to extract what it is inside each node – Badre Arras Jan 27 '15 at 22:29
  • 1
    This might be what you are looking for then. I apologize for the misunderstanding. http://stackoverflow.com/questions/8738031/deserializing-json-using-json-net-with-dynamic-data – James Shaw Jan 27 '15 at 22:30
  • i will try it nd let you know ^^ many thanks james ^^ – Badre Arras Jan 27 '15 at 22:35

1 Answers1

1

try this :

dynamic array = JsonConvert.DeserializeObject(jsonString );

  foreach(var item in array)
  {
    String Str =  item.Pins;
  }
user2933082
  • 263
  • 3
  • 10