无法获取 Json 数组中对象的值

新学习者

我在将对象值读入 Json 数组时遇到问题。请帮助我这里有什么问题。下面是我的代码。我正在发送员工列表以获取 Json 数组。但我没有在 json 数组中得到“EmployeeDetails”(即在第二级)。

这里有什么问题?

下面是我的代码

class Program
    {
        static void Main(string[] args)
        {
            List<Employee> list = new List<Employee>();
            Employee emp = new Employee { ID = 101, Department = "Stocks", EmployeeDetails = new Name { FirstName = "S", LastName = "Charles", Email = "[email protected]" } };
            Employee emp1 = new Employee { ID = 102, Department = "Stores", EmployeeDetails = new Name { FirstName = "L", LastName = "Dennis", Email = "[email protected]" } };
            list.Add(emp);
            list.Add(emp1);
            var resul1t = Program.GetEmployeeDetails(list);
        }
        private static string GetEmployeeDetails(List<Employee> emp)
        {

            string jsonarray = "";
            if ((emp != null) && (emp.Count > 0))
            {
                Dictionary<string, object> dic = new Dictionary<string, object>();
                int i = 0;
                foreach (var awo in emp)
                {
                    dic.Add(i.ToString(), ObjectToString(awo));
                    i++;
                }

                if (dic.Count > 0)
                {
                    jsonarray = DictionnaryToArray(dic);
                }
            }
            return jsonarray;
        }
        private static string ObjectToString(object obj)
        {
            Type objType = obj.GetType();
            IList<PropertyInfo> props = new List<PropertyInfo>(objType.GetProperties());
            StringBuilder sb = new StringBuilder(1024);
            foreach (PropertyInfo prop in props)
            {
                var type = prop.GetValue(obj, null);
                string attributeValueString = string.Format("\"{0}\":\"{1}\"", prop.Name, prop.GetValue(obj, null));

                if (type != null && type.GetType() == typeof(double))
                {
                    var doubleToStringValue = Convert.ToString(prop.GetValue(obj, null), System.Globalization.CultureInfo.InvariantCulture);
                    attributeValueString = string.Format("\"{0}\":\"{1:0.0}\"", prop.Name, doubleToStringValue);
                }
                sb.Append(attributeValueString).Append(";");
            }
            return "{" + sb.ToString().TrimEnd(new char[] { ';' }) + "}";
        }

        private static string DictionnaryToArray(Dictionary<string, object> data)
        {
            return "{" + string.Join(";", (from c in data select string.Format("\"{0}\":{1}", c.Key.ToString(), c.Value.ToString())).ToArray()) + "}";
        }
    }
    public class Employee
    {
        public int? ID { get; set; }
        public string Department { get; set; }
        public Name EmployeeDetails { get; set; }
    }
    public class Name
    {
        public string LastName { get; set; }
        public string Email { get; set; }
        public string FirstName { get; set; }
    }

谢谢

穆罕默德·萨吉德

ObjectToString当你有一个嵌套类时,你可以递归调用,我改变了一点点ObjectToString,就像下面的代码:

private static string ObjectToString(object obj)
{
    Type objType = obj.GetType();
    IList<PropertyInfo> props = new List<PropertyInfo>(objType.GetProperties());
    StringBuilder sb = new StringBuilder(1024);

    foreach (PropertyInfo prop in props)
    {
        string attributeValueString;
        var type = prop.GetValue(obj, null);

        if (type != null && type.GetType() == typeof(double))
        {
            var doubleToStringValue = Convert.ToString(prop.GetValue(obj, null), System.Globalization.CultureInfo.InvariantCulture);
            attributeValueString = string.Format("\"{0}\":\"{1:0.0}\"", prop.Name, doubleToStringValue);
        }//new code
        else if (prop.PropertyType.IsNested)
        {
            attributeValueString = string.Format("\"{0}\":{1}", prop.Name, ObjectToString(type));
        }
        else
        {
            attributeValueString = string.Format("\"{0}\":\"{1}\"", prop.Name, type);
        }
        sb.Append(attributeValueString).Append(",");
    }//updated code ; by ,
    return "{" + sb.ToString().TrimEnd(new char[] { ',' }) + "}";
}

请注意,您需要替换;by,才能获得有效的 json。

结果

{
   "0":{
      "ID":"101",
      "Department":"Stocks",
      "EmployeeDetails":{
         "LastName":"Charles",
         "Email":"[email protected]",
         "FirstName":"S"
      }
   },
   "1":{
      "ID":"102",
      "Department":"Stores",
      "EmployeeDetails":{
         "LastName":"Dennis",
         "Email":"[email protected]",
         "FirstName":"L"
      }
   }
}

我希望这可以帮助您解决问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从嵌套的 json 值对象中获取数组

来自分类Dev

从 Json 对象获取数组中的值

来自分类Dev

解析JSON以在javascript中的对象内部获取数组中的值

来自分类Dev

如何从JSON对象中的数组获取键名及其值

来自分类Dev

如何从数组中的JSON对象获取属性值并写入url

来自分类Dev

如何从嵌套的json数组中获取特定的对象值

来自分类Dev

如何使用 jq 从 json 数组/对象中获取多个值

来自分类Dev

如何从JSON响应中获取数组对象值?

来自分类Dev

如何从嵌套的 JSON 对象中获取检查值数组

来自分类Dev

无法从 Reactjs 中 JSON 对象内的数组访问值

来自分类Dev

无法获取JSON数组

来自分类Python

无法从python中的json数组获取嵌套对象

来自分类Dev

无法从打字稿中的 json 对象获取数组响应

来自分类Dev

无法获取 JSON 值

来自分类Dev

无法从JSON数组对象获取值

来自分类Dev

无法从JSON数组和对象获取值

来自分类Dev

无法从JSON数组和对象获取值

来自分类Dev

无法从 websocket 消息对象获取 json 值

来自分类Dev

我无法获取JSON对象的内部值

来自分类Dev

无法从JSON数组获取正确的值

来自分类Dev

无法在python中包含多个数组值的json对象中打印值

来自分类Dev

如何获取json对象和数组值?

来自分类Dev

如何获取 Json 对象数组值?

来自分类Dev

无法从res对象中获取json

来自分类Dev

无法获取JSON中的嵌套对象列表

来自分类Dev

无法从php获取javascript中的json对象

来自分类Dev

无法从json对象获取价值

来自分类Dev

无法获取JSON对象的长度

来自分类Dev

无法使用JSON获取对象