首页 新闻 会员 周边 捐助

关于List类型的json字符串反序列化的问题

0
悬赏园豆:10 [已解决问题] 解决于 2017-05-09 17:46
 Person person = JsonHelper.JsonToEntity<Person>(str);
                       var  customfields = JsonHelper.JsonToList<CustomFields>(str);
                       List<Images> image = JsonHelper.JsonToList<Images>(str);
                        c.Code = c.Code;
                        string sql = "insert into dbo.A(UserID,BatchID,CityCode,CustomFields,Images,Time,LineName,Note,PO_Code,Pile,Version,Lat,Lon,Alt,Type,CFCode)values('" 
                            + person.UserID + "','" + person.BatchID + "','" + person.CityCode + "','" + person.CustomFields + "','" 
                            + person.Images + "','" + person.Time + "','" + person.LineName + "','" + person.Note + "','" + person.PO_Code + "','" 
                            + person.Pile + "'," + person.Version + "," + person.Lat + "," + person.Lon + "," + person.Alt + "," + person.Type + ",'"
                            + customfields. + "')";
                       
                        SqlConnection conn = new SqlConnection(connStr);
                        
                        SqlCommand cmd = new SqlCommand(sql, conn);
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        conn.Close();
                        string path2 = "E:/国省道采集数据";
                        path = Environment.CurrentDirectory;
                        string pattern = "Command.txt";
                        string[] strFileName = Directory.GetFiles(path2, pattern);
                        foreach (var item in strFileName)
                         {
                        File.Delete(item);
                       
                         }
                         

                        //var xuliehua = str;
                   
                        //StreamReader sr = new StreamReader(@"E:\国省道采集数据\Command.txt", Encoding.GetEncoding("gb2312"));
                        //MessageBox.Show(sr.ToString());
                        
                   
                    }

                    toolStripProgressBar1.Value = 0;
                    MessageBox.Show("解压缩成功!");
                }
                else
                {
                    MessageBox.Show("警告:请选择要进行批量解压缩的文件!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch { }
                
        }

        private void txtfiles2_TextChanged(object sender, EventArgs e)
        {

        }
         [DataContract]

        public class Person
        {
            public string UserID { get; set; }
            public string BatchID { get; set; }
            public string CityCode { get; set; }
            public List<CustomFields> CustomFields { get; set; }
            public List<Images> Images { get; set; }
            public string Time { get; set; }
            public string LineName { get; set; }
            public string Note { get; set; }
            public string PO_Code { get; set; }
            public string Pile { get; set; }
            public int Version { get; set; }
            public double Lat { get; set; }
            public double Lon { get; set; }
            public double Alt { get; set; }
            public int Type { get; set; }
        }
         [Serializable]
         public class CustomFields
         {
             [DataMember]
             public string Code { get; set; }
             [DataMember]
             public string Value { get; set; }
         }
         [Serializable]
         public class Images
         {
             public string Time { get; set; }
             public string FileName { get; set; }
             public double Lon { get; set; }
             public double Lat { get; set; }
             public double Alt { get; set; }
         }

         public class JsonHelper
         {
             public static List<T> JsonToList<T>(string jsonStr)
             {
                 JavaScriptSerializer js = new JavaScriptSerializer();
                 //List<T> t = (List<T>)     js.Deserialize(str, typeof(List<T>));
                 List<T> t = js.Deserialize<List<T>>(jsonStr);
                 return t;
             }

             public static T JsonToEntity<T>(string jsonStr)
             {
                 JavaScriptSerializer js = new JavaScriptSerializer();
                 T t = js.Deserialize<T>(jsonStr);//(T)js.Deserialize(str, typeof(T));
                 return t;
             }
         }

字符串为{"UserID":"huangshan1@78","BatchID":"1489281487936","CityCode":"341000","CustomFields":[{"Code":"GLDWMC","Value":"徽州交通局"},{"Code":"QLMC","Value":"杨村2号桥"},{"Code":"QLBH","Value":"K3001"},{"Code":"JCSJ","Value":""},{"Code":"QLCD","Value":"134"},{"Code":"SJHZ","Value":"1"},{"Code":"AKJF","Value":"2"},{"Code":"AJCHNXF","Value":"1"},{"Code":"B_ISDANGER","Value":"2"},{"Code":"KJZC","Value":"125"},{"Code":"DKZDKJ","Value":"25"},{"Code":"QMQK","Value":"8.5"},{"Code":"QMJK","Value":"7.4"},{"Code":"LXBM","Value":"S103341000"},{"Code":"LDXLH","Value":"001"}],"Images":[{"Time":"Mar 12, 2017 12:00:00 AM","FileName":"/storage/emulated/0/easyroad/collectionpo/1489282287821.jpeg","Lon":118.1775853,"Lat":29.97436336,"Alt":0.0}],"Time":"2017-03-12","LineName":"S103341000","Note":"","PO_Code":"T_ql","Pile":"5.092","Version":2,"Lat":29.97436336,"Lon":118.1775853,"Alt":0.0,"Type":4}

现在person的可以正常转化,但是CustomFields这个就无法反序列化然后

customfields也点不出来东西
C#
姚刘乐的主页 姚刘乐 | 初学一级 | 园豆:70
提问于:2017-03-15 13:44
< >
分享
最佳答案
0

现在person的可以正常转化,但是CustomFields这个就无法反序列化然后

customfields也点不出来东西

 ---》》小伙,这里不是这么用的,要想取customfields中的数据,你得遍历啊!因为它是一个集合。。。Ienumerable怎么能直接点出来。

反序列化前首先你得弄清楚json数据是对象还是数组,然后再定义对应的实体,调对应的反序列化方法。json的两种数据类型,可以去了解下。

 
收获园豆:10
~扎克伯格 | 小虾三级 |园豆:1923 | 2017-03-17 16:54
其他回答(1)
0

你的json没问题,类没问题,那应该是反序列化出了问题。

试试我的:

            Person person = new Person();
            person = JsonConvert.DeserializeObject<Person>(json);

你的json中包含了三个类,所以它们是一个整体,要一起发反序列化。然后再从person对象中去取出里面的对象。

龙行天涯 | 园豆:1794 (小虾三级) | 2017-03-15 15:09

小伙,这个json里不是包含了三个类,你可以看下上面定义的person类的结构。

支持(0) 反对(0) ~扎克伯格 | 园豆:1923 (小虾三级) | 2017-03-17 16:52

@~扎克伯格: 一个Person,一个CustomFields,一个Images,json字符串里没有同时出现这三个类的信息?

支持(0) 反对(0) 龙行天涯 | 园豆:1794 (小虾三级) | 2017-03-17 19:11

@龙行天涯: 没有啊!我只看到了一段object格式的json中包含了UserID、BatchID等key name以及包含多个object的两个数组(CustomFields、Images)。

支持(0) 反对(0) ~扎克伯格 | 园豆:1923 (小虾三级) | 2017-03-21 10:26

@~扎克伯格: 你赢了

支持(0) 反对(0) 龙行天涯 | 园豆:1794 (小虾三级) | 2017-03-21 12:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册