求教C#解析JSON数据,急急急!!!

115639703xs 2014-05-16 03:02:09
{"closeDateTip":null,

"flightCountList":[
{"count":"1",
"dayOfWeek":"周五",
"flightDate":"2014-05-16",
"isSel":false,
"monthDay":"05月16日"},

{"count":"2","dayOfWeek":"周六","flightDate":"2014-05-17","isSel":true,"monthDay":"05月17日"},
{"count":"2","dayOfWeek":"周日","flightDate":"2014-05-18","isSel":false,"monthDay":"05月18日"},
{"count":"2","dayOfWeek":"周一","flightDate":"2014-05-19","isSel":false,"monthDay":"05月19日"},
{"count":"2","dayOfWeek":"周二","flightDate":"2014-05-20","isSel":false,"monthDay":"05月20日"},
{"count":"2","dayOfWeek":"周三","flightDate":"2014-05-21","isSel":false,"monthDay":"05月21日"},
{"count":"2","dayOfWeek":"周四","flightDate":"2014-05-22","isSel":false,"monthDay":"05月22日"},
{"count":"2","dayOfWeek":"周五","flightDate":"2014-05-23","isSel":false,"monthDay":"05月23日"},
{"count":"2","dayOfWeek":"周六","flightDate":"2014-05-24","isSel":false,"monthDay":"05月24日"},
{"count":"2","dayOfWeek":"周日","flightDate":"2014-05-25","isSel":false,"monthDay":"05月25日"}],

"flightList":[
{"arriveProvinceName":"湖北",
"arriveRegionName":"武汉",
"arriveStationName":null,
"flightDate":"2014-05-17",
"flightOnlineDetailId":"14051700678801000016",
"flightTime":"10:50",
"lastCount":36,
"onlineStatus":"Y",
"price":262,
"stationAddress":"中兴路1666号",
"stationName":"上海长途客运总站"},

{"arriveProvinceName":"湖北"
,"arriveRegionName":"武汉",
"arriveStationName":null,
"flightDate":"2014-05-17",
"flightOnlineDetailId":"14051700080121000016",
"flightTime":"17:00",
"lastCount":33,
"onlineStatus":"Y",
"price":262,
"stationAddress":"中兴路1666号",
"stationName":"上海长途客运总站"}],

"isClose":"N",
"isFlightCountDemo":true}


主要是要把这组数据封装到集合中
"flightList":[
{"arriveProvinceName":"湖北",
"arriveRegionName":"武汉",
"arriveStationName":null,
"flightDate":"2014-05-17",
"flightOnlineDetailId":"14051700678801000016",
"flightTime":"10:50",
"lastCount":36,
"onlineStatus":"Y",
"price":262,
"stationAddress":"中兴路1666号",
"stationName":"上海长途客运总站"},
...全文
101 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
115639703xs 2014-05-16
  • 打赏
  • 举报
回复
谢谢!各位大虾的帮助!问题已经解决,非常感谢!
ArmStronger 2014-05-16
  • 打赏
  • 举报
回复
引用 5 楼 u014234840 的回复:
不好意思我下的那个DLL里面没有JsonConvert.SerializeObject<RootObject>(Json字符串);这个 我的只有 JosnList object3 = JsonConvert.DeserializeObject<JosnList>(GetJosn()); 关键这个还未将对象引用到实例
是的,上面写错了,在3搂纠正过来了。 JosnList object3 = JsonConvert.DeserializeObject<JosnList>(GetJosn()); 你的JosnList类型要是我上面的实体类RootObject结构,GetJosn()返回的要是你上面的Json字符串,这样的话应该不会错
115639703xs 2014-05-16
  • 打赏
  • 举报
回复
不好意思我下的那个DLL里面没有JsonConvert.SerializeObject<RootObject>(Json字符串);这个 我的只有 JosnList object3 = JsonConvert.DeserializeObject<JosnList>(GetJosn()); 关键这个还未将对象引用到实例
exception92 2014-05-16
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Json;
using System.Reflection;
using System.IO;

namespace test
{
    /// <summary>
    /// JSONHelper类
    /// </summary>
    public class JSONHelper
    {
        /// <summary>
        /// 把对象序列化 JSON 字符串
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="obj">对象实体</param>
        /// <returns>JSON字符串</returns>
        public static string GetJson<T>(T obj)
        {
            //记住 添加引用 System.ServiceModel.Web
            /**
             * 如果不添加上面的引用,System.Runtime.Serialization.Json; Json是出不来的哦
             * */
            DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(T));
            using (MemoryStream ms = new MemoryStream())
            {
                json.WriteObject(ms, obj);
                string szJson = Encoding.UTF8.GetString(ms.ToArray());
                return szJson;
            }
        }

        /// <summary>
        /// 把JSON字符串还原为对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="szJson">JSON字符串</param>
        /// <returns>对象实体</returns>
        public static T ParseFormJson<T>(string szJson)
        {
            T obj = Activator.CreateInstance<T>();
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(szJson)))
            {
                DataContractJsonSerializer dcj = new DataContractJsonSerializer(typeof(T));
                return (T)dcj.ReadObject(ms);
            }
        }
    }
}
ArmStronger 2014-05-16
  • 打赏
  • 举报
回复
上面解析笔误了,应该是
RootObject  object = JsonConvert.DeserializeObject<RootObject>(Json字符串);
wg5945 2014-05-16
  • 打赏
  • 举报
回复
RootObject r = JsonConvert.DeserializeObject<RootObject>(json); public class FlightCountList { public string count { get; set; } public string dayOfWeek { get; set; } public string flightDate { get; set; } public bool isSel { get; set; } public string monthDay { get; set; } } public class FlightList { public string arriveProvinceName { get; set; } public string arriveRegionName { get; set; } public object arriveStationName { get; set; } public string flightDate { get; set; } public string flightOnlineDetailId { get; set; } public string flightTime { get; set; } public int lastCount { get; set; } public string onlineStatus { get; set; } public int price { get; set; } public string stationAddress { get; set; } public string stationName { get; set; } } public class RootObject { public object closeDateTip { get; set; } public List<FlightCountList> flightCountList { get; set; } public List<FlightList> flightList { get; set; } public string isClose { get; set; } public bool isFlightCountDemo { get; set; } }
ArmStronger 2014-05-16
  • 打赏
  • 举报
回复
实体类:
public class FlightCountList
{
    public string count { get; set; }
    public string dayOfWeek { get; set; }
    public string flightDate { get; set; }
    public bool isSel { get; set; }
    public string monthDay { get; set; }
}

public class FlightList
{
    public string arriveProvinceName { get; set; }
    public string arriveRegionName { get; set; }
    public object arriveStationName { get; set; }
    public string flightDate { get; set; }
    public string flightOnlineDetailId { get; set; }
    public string flightTime { get; set; }
    public int lastCount { get; set; }
    public string onlineStatus { get; set; }
    public int price { get; set; }
    public string stationAddress { get; set; }
    public string stationName { get; set; }
}

public class RootObject
{
    public object closeDateTip { get; set; }
    public List<FlightCountList> flightCountList { get; set; }
    public List<FlightList> flightList { get; set; }
    public string isClose { get; set; }
    public bool isFlightCountDemo { get; set; }
}
然后可以用Newtonsoft.Json 解析就好了
RootObject  object = JsonConvert.SerializeObject<RootObject>(Json字符串);

110,536

社区成员

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

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

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