Asp.net my life
Asp.net my life

Reputation: 386

How to check dynamically whether an index in the array exists or not?

enter image description here

I have deserialized a JSON string into arrays of current_location, EducationHistory and WorkHistory The problem i am encountering is that different people might have different specifications of education history and therefore increasing or decreasing the number of indexes in the array

How do i resolve dynamically this problem and store the values in the database

Edit:

Result soap = serializer.deserialize<Result>(ser);
      foreach(var data in soap.data)
      {
         int lenght= soap.data.educationhistory.Length;
         foreach(var education in soap.data.educationhistory)
            {
                 // my insert query
            }
      }

Now the problem is I will have to run a foreach loop for work_history also. Two foreach loop inside a foreach loop is an issue. How can it be done in minimum use of loops??

Upvotes: 1

Views: 2428

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039498

You could check the length of the array:

int length = soap.data.education_history.Length; // gives you 2

Once you know the length you know that indexes must be smaller than this length. Of course if you simply loop through this array you won't have any problems with indexes:

foreach (var education in soap.data.education_history)
{
    // TODO: save the education in your database
}

Upvotes: 1

Related Questions