我有一個JSON對象,我已經反序列化到服務器端的對象。其中一個對象是對象的數組列表,每個對象都是一個字符串數組的字典。在我的觀察中,我可以深入瞭解對象並查看對象中的所有內容,但我無法弄清楚如何訪問代碼中對象內的數據。我需要遍歷每個對象並訪問字典中的鍵和值。 Rigor對象就是我正在努力鑽研的內容。C#訪問對象內的數據
我已經硬編碼什麼樣的數據看起來像在未來的一個例子。
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]
}
訪問該數據的哪個部分是您遇到的問題? – 2014-12-04 14:44:27
我試過你的代碼,看起來像是有效的。究竟是什麼問題?你能添加更多細節嗎? – 2014-12-04 14:44:55
AssessmentWCFVariables.Rigor成員的類型是什麼? 例如,如果你這樣做,輸出是什麼? 'AssessmentWCFVariables awv = [無論哪裏來自]; object o = awv.Rigor [0]; System.Console.WriteLine((typeof o).ToString());' – 2014-12-04 14:46:06