Reputation: 421
I have a JSON object that I have deserialized into an object on the server side. One of the objects is an arraylist of objects and each object is a dictionary of string, array. In my watch I can drill down through the objects and see everything that is in the object but I can't figure out how to access the data inside the objects in code. I need to loop through each object and access the keys and values inside the dictionaries. The Rigor object is what I'm trying to drill down into.
I have hard coded an example of what the data looks like coming in.
string rigorSelection = "{\"StandardID\":165638," +
"\"ItemTypes\":[\"Multiple Choice\",\"True/False\",\"Constructed Response\",\"Explicit Constructed Response\",\"Select Dropdown\",\"Evidence Based Selected Response\",\"Drag and Drop\",\"Selectable Text\",\"Multi-Part\",\"Graphing\",\"Matching\"], " +
"\"TotalItemCount\":10," +
"\"StandardName\":\"CCSS.9_12.MA.N.RN.2\"," +
"\"Rigor\":[" +
"{\"Remembering\":[0,0,0,0,0,0,0,0,0,0,0]}," +
"{\"Understanding\":[0,0,0,0,0,0,0,0,0,0,0]}," +
"{\"Applying\":[0,0,0,0,0,0,0,0,0,0,0]}," +
"{\"Analyzing\":[0,0,0,0,0,0,0,0,0,0,0]}," +
"{\"Evaluating\":[0,0,0,0,0,0,0,0,0,0,0]}," +
"{\"Creating\":[0,0,0,0,0,0,0,0,0,0,0]}," +
"{\"Not Specified\":[0,0,0,0,0,0,0,0,0,0,0]}]" +
"}";
System.Web.Script.Serialization.JavaScriptSerializer jSerializer =
new System.Web.Script.Serialization.JavaScriptSerializer();
AssessmentWCFVariables assessmentWCFVariable = jSerializer.Deserialize<AssessmentWCFVariables>(rigorSelection);
var rigorCounts = assessmentWCFVariable.Rigor;
The class that the data is being deserialized to is this...
[Serializable]
[DataContract]
public class AssessmentWCFVariables
{
[DataMember]
public int StandardID { get; set; }
[DataMember]
public string StandardName { get; set; }
[DataMember]
public ArrayList ItemTypes { get; set; }
[DataMember]
public ArrayList Rigor { get; set; }
[DataMember]
public int TotalItemCount { get; set; }
}
UPDATE rigorCounts is an ArrayList of objects. I can loop though each object but everything I try to access the Key /Value pairs in the dictionaries give me an error. I am trying to do this.
foreach (var rigor in rigorCounts)
{
var key = //code to get key of rigor i.e Remembering
var value = //code to get the arrary i.e. [0,0,0,0,0,0,0,0,0,0,0]
}
Upvotes: 1
Views: 3994
Reputation: 14274
I need to loop through each object and access the keys and values inside the dictionaries.
If you consider the objects in your Rigor object dictionaries than you should change the type of your Rigor object to:
[DataMember]
public List<Dictionary<string, Int32[]>> Rigor { get; set; }
Then you can easily loop through each dictionary in the Rigor object with the following code:
var rigorCounts = assessmentWCFVariable.Rigor;
foreach (Dictionary<string, Int32[]> rigorCountDict in rigorCounts)
{
foreach (KeyValuePair<string, Int32[]> kvpair in rigorCountDict)
{
string key = kvpair.Key; // e.g. "Remembering"
Int32[] value = kvpair.Value; // e.g. [0,0,0,0,0,0,0,0,0,0,0]
}
}
Just in case you don't have the luxury of changing your data-contracts to a List of Dictionaries you can do the following on the original Rigor ArrayList:
foreach (Dictionary<string, object> rigorCountDict in rigorCounts)
{
foreach (KeyValuePair<string, object> kvpair in rigorCountDict)
{
string key = kvpair.Key; // e.g. "Remembering"
Int32[] value = (Int32[])((ArrayList)kvpair.Value).ToArray(typeof(Int32)); // e.g. [0,0,0,0,0,0,0,0,0,0,0]
}
}
It is less elegant than the first example, but it works as well.
Upvotes: 1
Reputation: 597
I agree that a separate Rigor class would be the best way to do it. But to answer your specific question:
foreach (var item in assessmentWCFVariable.Rigor)
{
var dictionary = item as Dictionary<string, int[]>;
if (dictionary == null) continue;
// Do what you want with the dictionary here
}
It is possible that the following change needs to be made to the AssesmentClass:
[DataMember]
public IEnumerable<Dictionary<string, int[]> Rigor { get; set; }
Upvotes: 1
Reputation: 45155
You are using an ArrayList
for Rigor
. When you access that (for example, by index) you will get an object that needs to be cast before you can access it's properties. It would probably be better to create a class for Rigor
objects and use a List<Rigor>
. Something like:
public class Rigor
{
public List<int> Remembering { get; set; }
public List<int> Understanding {get; set; }
// an so on for other properties...
}
And then change your AssessmentWCFVariables
to have:
[DataMember]
public List<Rigor> Rigor { get; set; }
IMHO: there is zero reason to ever use ArrayList
since the introduction of the generic List<T>
Upvotes: 0