我有一个JSON对象,我已将其反序列化为服务器端的对象。其中一个对象是对象的arraylist,每个对象是string,array的字典。在我的手表中,我可以向下钻取对象并查看对象中的所有内容,但我无法弄清楚如何在代码中访问对象内的数据。我需要遍历每个对象并访问字典中的键和值。 Rigor对象就是我试图深入研究的对象。
我已经硬编码了数据的样子。
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;
数据被反序列化的类是这个......
[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是一个对象的ArrayList。我可以遍历每个对象,但我尝试访问字典中的键/值对的所有内容都会给我一个错误。我想这样做。
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]
}
答案 0 :(得分:1)
我同意单独的Rigor课程是最好的方法。但要回答你的具体问题:
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
}
可能需要对AssesmentClass进行以下更改:
[DataMember]
public IEnumerable<Dictionary<string, int[]> Rigor { get; set; }
答案 1 :(得分:1)
我需要遍历每个对象并访问字典中的键和值。
如果您考虑Rigor对象词典中的对象,则应将Rigor对象的类型更改为:
[DataMember]
public List<Dictionary<string, Int32[]>> Rigor { get; set; }
然后,您可以使用以下代码轻松遍历Rigor对象中的每个字典:
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]
}
}
如果您没有将数据合同更改为字典列表,您可以在原始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]
}
}
它不如第一个例子那么优雅,但也可以。
答案 2 :(得分:0)
您正在使用ArrayList
Rigor
。当您访问它时(例如,通过索引),您将获得一个需要进行转换的对象,然后才能访问它的属性。为Rigor
个对象创建一个类并使用List<Rigor>
可能会更好。类似的东西:
public class Rigor
{
public List<int> Remembering { get; set; }
public List<int> Understanding {get; set; }
// an so on for other properties...
}
然后将您的AssessmentWCFVariables
更改为:
[DataMember]
public List<Rigor> Rigor { get; set; }
恕我直言:自从引入通用ArrayList
List<T>