1

I have a simple serialized json array

string json = "[{\"id\":100,\"UserId\":99},{\"id\":101,\"UserId\":98}]";
var data = (List<Model>)Newtonsoft.Json.JsonConvert.DeserializeObject(json , typeof(List<Model>));

my model to deserialize:

public class Model 
{ 
    public int? id { get; set; }
    public int? UserId { get; set; }
}

What is the best way to retrieve data from each Index[?] and print it to Console ?

2
  • @devopsEMK I know how to deserialize . it works successfully but I having issue fetching data from index as i cant compare with data.length in a for loop. Commented Mar 22, 2016 at 14:24
  • 2
    What's the problem with using data[n] and data.Count, exactly? Also you can get rid of the cast by using var data = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Model>>(json); instead. Commented Mar 22, 2016 at 14:27

2 Answers 2

2

You can do a foreach loop:

foreach(var item in data) {
    Console.WriteLine(item.UserId);
}
Sign up to request clarification or add additional context in comments.

Comments

1
 string json = "[{\"id\":100,\"UserId\":99},{\"id\":101,\"UserId\":98}]";
        var objects = JArray.Parse(json);
        var firstIndexValue = objects[0];
        Console.WriteLine(firstIndexValue);

        foreach (var index in objects)
        {
            Console.WriteLine(index);
        }
for (int index = 0; index < objects.Count; index++)
        {
            Console.WriteLine(objects[index]);
        }

1 Comment

thanks for the help . But what if i don't know the index value instead i want to print each index value one by one ?

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.