c# JSON反序列化问题

聆听灬莫问 2013-08-12 12:04:47
c# 如何读取这个JSON格式的数组啊。。。这种的我能读到,那种的怎么得到啊。。。。求教
...全文
624 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
知道是什么问题了,那个地方是数组, 不过数组的话又要怎么读取呢?
  • 打赏
  • 举报
回复
就是这句话 s = obj.result.toreturn.memberId.ToString();
  • 打赏
  • 举报
回复
我在最后取值的之后出错的,提示 未将对象引用设置到对象的实例。 string s; DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(GetAccessCompanyM)); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(html))) { GetAccessCompanyM obj = js.ReadObject(ms) as GetAccessCompanyM; //Console.WriteLine(obj.fruits.a); s = obj.result.toreturn.memberId.ToString(); }
swdmike 2014-01-08
  • 打赏
  • 举报
回复
用DataContractJsonSerializer很方便的
u011638080 2013-08-12
  • 打赏
  • 举报
回复
            string jsonStr = @"{""fruits"":{""a"":""orange"",""b"":""banana"",""c"":""apple""},""name"":{""z"":""zhangsan"",""l"":""lisi""}}";

            JavaScriptSerializer serializer = new JavaScriptSerializer();

            Dictionary<string, object> json = serializer.DeserializeObject(jsonStr) as Dictionary<string, object>;

            Dictionary<string, object> obj = json["fruits"] as Dictionary<string, object>;

            Console.WriteLine(obj["a"]);

            Console.ReadLine();
看看
聆听灬莫问 2013-08-12
  • 打赏
  • 举报
回复
引用 5 楼 u011638080 的回复:
C#里面对json的处理有2种,JavaScriptSerializer和DataContractJsonSerializer。 JavaScriptSerializer读出来就是key-value pair这种Dictionary<string, object>的东西,这个比较通用。 DataContractJsonSerializer类似于其他的Serializer,比如XMLSerializer一类的,是对象序列化/反序列化用的,所以你看到上面要先定义一个Obj对象,把json字串里面的key都定义成属性,最后执行反序列化就出来了一个Obj。
谢谢哈,我弄好了
u011638080 2013-08-12
  • 打赏
  • 举报
回复
C#里面对json的处理有2种,JavaScriptSerializer和DataContractJsonSerializer。 JavaScriptSerializer读出来就是key-value pair这种Dictionary<string, object>的东西,这个比较通用。 DataContractJsonSerializer类似于其他的Serializer,比如XMLSerializer一类的,是对象序列化/反序列化用的,所以你看到上面要先定义一个Obj对象,把json字串里面的key都定义成属性,最后执行反序列化就出来了一个Obj。
聆听灬莫问 2013-08-12
  • 打赏
  • 举报
回复
引用 3 楼 u011638080 的回复:
既然你诚心诚意的问了,哥还是大发慈悲的告诉你好了。 捂好蛋蛋,再看下面,不然会很疼的
        [DataContract]
        public class DanTengObj
        {
            [DataMember(Order = 1, Name = "fruits")]
            public Fruits fruits { get; set; }
            [DataMember(Order = 2, Name = "name")]
            public Name name { get; set; }

            [DataContract]
            public class Fruits
            {
                [DataMember(Order = 1, Name = "a")]
                public string a { get; set; }
                [DataMember(Order = 2, Name = "b")]
                public string b { get; set; }
                [DataMember(Order = 3, Name = "c")]
                public string c { get; set; }
            }

            [DataContract]
            public class Name
            {
                [DataMember(Order = 1, Name = "z")]
                public string z { get; set; }
                [DataMember(Order = 1, Name = "l")]
                public string l { get; set; }
            }
        }

        static void DanTengJsonTest()
        {
            string jsonStr = @"{""fruits"":{""a"":""orange"",""b"":""banana"",""c"":""apple""},""name"":{""z"":""zhangsan"",""l"":""lisi""}}";

            DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(DanTengObj));

            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonStr)))
            {
                DanTengObj obj = js.ReadObject(ms) as DanTengObj;
                Console.WriteLine(obj.fruits.a);
            }
        }
[DataContract] public class DanTengObj { [DataMember(Order = 1, Name = "fruits")] public Fruits fruits { get; set; } [DataMember(Order = 2, Name = "name")] public Name name { get; set; } [DataContract] public class Fruits { [DataMember(Order = 1, Name = "a")] public string a { get; set; } [DataMember(Order = 2, Name = "b")] public string b { get; set; } [DataMember(Order = 3, Name = "c")] public string c { get; set; } } [DataContract] public class Name { [DataMember(Order = 1, Name = "z")] public string z { get; set; } [DataMember(Order = 1, Name = "l")] public string l { get; set; } } }
这个怎么理解啊
u011638080 2013-08-12
  • 打赏
  • 举报
回复
既然你诚心诚意的问了,哥还是大发慈悲的告诉你好了。 捂好蛋蛋,再看下面,不然会很疼的
        [DataContract]
        public class DanTengObj
        {
            [DataMember(Order = 1, Name = "fruits")]
            public Fruits fruits { get; set; }
            [DataMember(Order = 2, Name = "name")]
            public Name name { get; set; }

            [DataContract]
            public class Fruits
            {
                [DataMember(Order = 1, Name = "a")]
                public string a { get; set; }
                [DataMember(Order = 2, Name = "b")]
                public string b { get; set; }
                [DataMember(Order = 3, Name = "c")]
                public string c { get; set; }
            }

            [DataContract]
            public class Name
            {
                [DataMember(Order = 1, Name = "z")]
                public string z { get; set; }
                [DataMember(Order = 1, Name = "l")]
                public string l { get; set; }
            }
        }

        static void DanTengJsonTest()
        {
            string jsonStr = @"{""fruits"":{""a"":""orange"",""b"":""banana"",""c"":""apple""},""name"":{""z"":""zhangsan"",""l"":""lisi""}}";

            DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(DanTengObj));

            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonStr)))
            {
                DanTengObj obj = js.ReadObject(ms) as DanTengObj;
                Console.WriteLine(obj.fruits.a);
            }
        }
聆听灬莫问 2013-08-12
  • 打赏
  • 举报
回复
引用 1 楼 u011638080 的回复:
            string jsonStr = @"{""fruits"":{""a"":""orange"",""b"":""banana"",""c"":""apple""},""name"":{""z"":""zhangsan"",""l"":""lisi""}}";

            JavaScriptSerializer serializer = new JavaScriptSerializer();

            Dictionary<string, object> json = serializer.DeserializeObject(jsonStr) as Dictionary<string, object>;

            Dictionary<string, object> obj = json["fruits"] as Dictionary<string, object>;

            Console.WriteLine(obj["a"]);

            Console.ReadLine();
看看
不用字典的话 有方法不

111,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧