本帖最后由 adingtao11 于 2012-01-07 12:55:15 编辑

解决方案 »

  1.   

     private string ToJson<T>(T obj)
        {
            DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream();
            ds.WriteObject(ms, obj);
            string strJSON = Encoding.UTF8.GetString(ms.ToArray());
            ms.Close();
            return strJSON;
        }    private T Deserialize<T>(string sJson) where T : class
        {
            DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(sJson));
            T obj = (T)ds.ReadObject(ms);        ms.Close();
            return obj;
        }http://www.cnblogs.com/dlonghow/archive/2009/02/19/1393702.html
      

  2.   

    http://www.dotnetdev.cn/2010/01/c%E4%B8%AD%E7%9A%84json%E7%9A%84%E5%BA%8F%E5%88%97%E5%8C%96%E5%92%8C%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96/
      

  3.   

    主要是{"item": 和,"title":"上海 - 北京 共47次车:"} 这里怎么处理 
    最好构造一个相应的类
      

  4.   

    因为没有最全的数据,所以item里数据难以判断是否作成一个class。(如果item里第二个数组是最全的数据的话,那么可以把里面的key全抽出来作为属性)保险起见用个 List<Dictionary<string, string>> 是没问题的。public class Info
    {
        public string title { get; set; }
        public List<Dictionary<string, string>> item { get; set; }
    }
      

  5.   

    主要是{"item": 和,"title":"上海 - 北京 共47次车:"} 这里怎么处理
      

  6.   


    这个类就能反序列啦。试了吗?var info = JsonConvert.Deserialize<Info>(json);
    Console.WriteLine(info.title);
    Console.WriteLine(info.item[0]["startStation"]);
      

  7.   

    看着数据去构造!字段跟值之间是使用:号分隔的,而json的值只有五种:数值、字符串、bool值、数组、另外一个json对象。根本没有丰富的类型。你这个开发人员有点“滥”,把一些数值字段定义为字符串了。它类似于
    public class BType
    {
        public string trainCode;
        public string startStation;
        public string arriveStation;
        public string startTime;
        public string endTime;
        public string takeTime;
        public string trainGrade;
        public string mileage;
        public string hard-seat;
        public string hard-seat-prc;
        public string hard-sleeper1;
        public string hard-sleeper1-prc;
        public string hard-sleeper2;
        public string hard-sleeper2-prc;
        public string hard-sleeper3;
        public string hard-sleeper3-prc;
    }public class AType
    {
        public BType[] item;
        public string title;
    }
      

  8.   


    缺少字段不会影响 DataContractJsonSerializer 的正常运行,不会抛出异常。同样地就算多定义了字段也不会异常。比如说事后核对发现缺少vag-sleeper1-prc字段,那么在BType里边加上这个字段,再次重新运行整个程序,就行了!