Reputation: 13
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
Upvotes: 0
Views: 89
Reputation: 5203
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
}
Upvotes: 0
Reputation: 1313
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.
Upvotes: 1
Reputation: 6157
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
.
Upvotes: 0