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"]
        },
......
}
13
  • What do you mean by "without using a helper class"? Do you mean "without using a strongly-typed deserialization-target object"? Commented Jan 27, 2015 at 22:03
  • 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. Commented Jan 27, 2015 at 22:23
  • 1
    Can you parse the json to a dictionary for example? Then you can access via Key/Value pairs. Commented Jan 27, 2015 at 22:26
  • 1
    Would this be more what you are looking for? stackoverflow.com/questions/13683757/… Commented Jan 27, 2015 at 22:28
  • 1
    This might be what you are looking for then. I apologize for the misunderstanding. stackoverflow.com/questions/8738031/… Commented Jan 27, 2015 at 22:30

1 Answer 1

1

try this :

dynamic array = JsonConvert.DeserializeObject(jsonString );

  foreach(var item in array)
  {
    String Str =  item.Pins;
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.