0

enter image description here I tried to get the value of an object[] but using foreach didn't work well. object[] contains a list of object[] how can I fetch the data

public void SaveBottonTable(string dimension)
{
    JavaScriptSerializer json_serializer = new JavaScriptSerializer();
    object[] routes_list = 
        (object[])json_serializer.DeserializeObject(dimension);
                GlobalConstant.holeconfiguration = routes_list;//list is referred in the image
    foreach(object hole in routes_list)
    {
        hole[0]//shows error
    }
}

how to get the value of the first object[]

      https://i.sstatic.net/x8SG1.png
2
  • object hole represents single object. when you do hole[0] you are trying to use single object as array of objects. That's why you are getting an error. You should do var routes = hole as object[]; in foreach loop an then you can access individual objects from routes by doing routes[0], routes[1] etc.. Commented Apr 24, 2019 at 7:27
  • Posting the json would be nice Commented Apr 24, 2019 at 7:39

3 Answers 3

1

Since each element is an array of strings you should convert each object to array and then index it.

foreach(object hole in routes_list)
{
   var elements = hole.ToArray();
//then you can access elements[0] and elements [1]
}

However, I think it would be better if the format was "Key":"Value" Example: { "object-ID":"1234123cfrewr", "view" : "Cover", . . .}

Be careful with Collection.Generics since you have a couple of Dictionaries there. They will need extra care.

Sign up to request clarification or add additional context in comments.

Comments

0

It looks like your json contains a dictionary. In that case I would deserialize it to the appropriate type Dictionary<string, object>. Then when looping through it you can either get the key using hole.Key or the value using hole.Value.

Comments

0

This way:

object[] list = new object[]
{
    new object(),
    1,
    "ciao",
    new object[] { 1, 2, 3, 4, 5 },
    "pizza",
    new object[] { "ciao", "pizza" },
    new List<object>(){ 123, 456 }
};

foreach (ICollection collection in list.OfType<ICollection>())
{
    //Here you can work with every collection found in your list
}

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.