2014-12-04 68 views
1

我有一個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] 
     } 
+1

訪問該數據的哪個部分是您遇到的問題? – 2014-12-04 14:44:27

+1

我試過你的代碼,看起來像是有效的。究竟是什麼問題?你能添加更多細節嗎? – 2014-12-04 14:44:55

+2

AssessmentWCFVariables.Rigor成員的類型是什麼? 例如,如果你這樣做,輸出是什麼? 'AssessmentWCFVariables awv = [無論哪裏來自]; object o = awv.Rigor [0]; System.Console.WriteLine((typeof o).ToString());' – 2014-12-04 14:46:06

回答

1

我需要遍歷每個對象並訪問字典內的鍵和值。

如果您考慮在嚴密對象字典中的對象應該比你改變你的嚴謹性對象的類型:

[DataMember] 
    public List<Dictionary<string, Int32[]>> Rigor { get; set; } 

然後你可以通過嚴謹的對象每個字典有以下容易循環代碼:

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] 
    } 
} 

萬一你沒有改變你的數據合同詞典列表的奢侈,你可以做原來的嚴謹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] 
    } 
} 

它比第一個例子不那麼優雅,但它也可以。

+0

如果'rigor'對象中的鍵是可變的(即有時候可能有'Remembering'鍵,有時候不是,有時甚至是完全意想不到的),那麼這是絕對正確的方法。如果它們是固定的,那麼創建一個強類型對象可能更乾淨。 – 2014-12-04 14:52:51

1

我同意一個單獨的Rigor類是最好的辦法。

[DataMember] 
public IEnumerable<Dictionary<string, int[]> Rigor { get; set; } 
0

您使用的是ArrayListRigor:但是,爲了回答您的具體問題:

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作出。當你訪問它(例如,通過索引)時,你會得到一個需要被轉換的對象,然後才能訪問它的屬性。創建一個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>