C# 访问对象内的数据

本文关键字:数据 对象 访问 | 更新日期: 2023-09-27 18:33:03

我有一个JSON对象,我已经反序列化为服务器端的对象。 其中一个对象是对象的数组列表,每个对象都是字符串数组的字典。 在我的手表中,我可以向下钻取对象并查看对象中的所有内容,但我无法弄清楚如何在代码中访问对象内部的数据。 我需要遍历每个对象并访问字典中的键和值。 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; }
}     

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

C# 访问对象内的数据

我需要遍历每个对象并访问字典中的键和值。

如果您考虑 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]
    }
}

它不如第一个例子那么优雅,但它也可以工作。

我同意一个单独的 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
}

可能需要对评估类进行以下更改:

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

您正在使用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; }

恕我直言:自从引入通用List<T>以来,没有理由使用ArrayList