首页 新闻 会员 周边 捐助

jason反序列化 将jason串转换成 对象

0
悬赏园豆:5 [已解决问题] 解决于 2014-06-27 16:33

一个eventInfo对象下面有多个presentation对象

将下面的jason串转换成 eventInfo和presentation对象

{
"eventInfo": [
{
"eiId": 1,
"eiEventName": "test_event_1",
"eiEventTypeId": 5,
"eiCreaterId": 1,
"eiStartTime": "2014-06-19 10:30:00.0",
"eiEndTime": "2014-06-19 11:30:00.0",
"eiCreateDate": "2014-06-10 10:28:38.0",
"eiUpdateDate": "2014-06-24 11:09:59.0",
"eiDeleteFlg": 0,
"presentation": [
{
"pExtractPointFlg": 0,
"piCreateDate": "2014-06-16 14:00:45.0",
"piCreaterId": 1,
"piDeleteFlg": 0,
"piEndTime": "2014-06-17 10:28:27.0",
"piEventId": 1,
"piExtractNewFlg": 0,
"piId": "304d3bf393234734b252a6e6a20b62bd",
"piLanguage": 1,
"piPptName": "dddd",
"piPresentationName": "111111111",
"piPresentorMail": "wuxu@test.com",
"piPresentorName": "wuxu_DDDD",
"piShareFlag": 0,
"piSharePDFName": "",
"piStartTime": "2014-06-17 10:28:27.0",
"piStatus": 0,
"piUpdateDate": "2014-06-16 14:00:45.0"
},
{
"pExtractPointFlg": 0,
"piCreateDate": "2014-06-16 09:52:52.0",
"piCreaterId": 1,
"piDeleteFlg": 0,
"piEndTime": "2014-06-17 10:28:27.0",
"piEventId": 1,
"piExtractNewFlg": 0,
"piId": "40c5592085194aa488c3e2aede028ea3",
"piLanguage": 1,
"piPptName": "dddd",
"piPresentationName": "333333333",
"piPresentorMail": "wuxu@test.com",
"piPresentorName": "wuxu_DDDD",
"piShareFlag": 0,
"piSharePDFName": "",
"piStartTime": "2014-06-17 10:28:27.0",
"piStatus": 0,
"piUpdateDate": "2014-06-16 13:21:19.0"
},
{
"pExtractPointFlg": 0,
"piCreateDate": "2014-06-16 14:00:47.0",
"piCreaterId": 1,
"piDeleteFlg": 0,
"piEndTime": "2014-06-17 10:28:27.0",
"piEventId": 1,
"piExtractNewFlg": 0,
"piId": "818736085d7840b1a16425e181f44d6c",
"piLanguage": 1,
"piPptName": "dddd",
"piPresentationName": "444444444",
"piPresentorMail": "wuxu@test.com",
"piPresentorName": "wuxu_DDDD",
"piShareFlag": 0,
"piSharePDFName": "",
"piStartTime": "2014-06-17 10:28:27.0",
"piStatus": 0,
"piUpdateDate": "2014-06-16 14:00:47.0"
},
{
"pExtractPointFlg": 0,
"piCreateDate": "2014-06-16 14:00:45.0",
"piCreaterId": 1,
"piDeleteFlg": 0,
"piEndTime": "2014-06-17 10:28:27.0",
"piEventId": 1,
"piExtractNewFlg": 0,
"piId": "d57a7e5eb1b84e87a3ee179523db1560",
"piLanguage": 1,
"piPptName": "dddd",
"piPresentationName": "666666666",
"piPresentorMail": "wuxu@test.com",
"piPresentorName": "wuxu_DDDD",
"piShareFlag": 0,
"piSharePDFName": "",
"piStartTime": "2014-06-17 10:28:27.0",
"piStatus": 0,
"piUpdateDate": "2014-06-16 14:00:45.0"
},
{
"pExtractPointFlg": 0,
"piCreateDate": "2014-06-16 14:00:43.0",
"piCreaterId": 1,
"piDeleteFlg": 0,
"piEndTime": "2014-06-17 10:28:27.0",
"piEventId": 1,
"piExtractNewFlg": 0,
"piId": "fc96d0e48eb342b089dde47116744533",
"piLanguage": 1,
"piPptName": "dddd",
"piPresentationName": "999999999",
"piPresentorMail": "wuxu@test.com",
"piPresentorName": "wuxu_DDDD",
"piShareFlag": 0,
"piSharePDFName": "",
"piStartTime": "2014-06-17 10:28:27.0",
"piStatus": 0,
"piUpdateDate": "2014-06-16 14:00:43.0"
}
]
},
{
"eiId": 2,
"eiEventName": "test_event_2",
"eiEventTypeId": 1,
"eiCreaterId": 2,
"eiStartTime": "2014-06-17 10:28:27.0",
"eiEndTime": "2014-06-17 10:28:27.0",
"eiCreateDate": "2014-06-10 15:09:13.0",
"eiUpdateDate": "2014-06-16 13:21:41.0",
"eiDeleteFlg": 0,
"presentation": [
{
"pExtractPointFlg": 0,
"piCreateDate": "2014-06-16 14:00:46.0",
"piCreaterId": 1,
"piDeleteFlg": 0,
"piEndTime": "2014-06-17 10:28:27.0",
"piEventId": 2,
"piExtractNewFlg": 0,
"piId": "3a587d14d8de419c8f63ce2e9fcfc20f",
"piLanguage": 1,
"piPptName": "dddd",
"piPresentationName": "222222222",
"piPresentorMail": "wuxu@test.com",
"piPresentorName": "wuxu_DDDD",
"piShareFlag": 0,
"piSharePDFName": "",
"piStartTime": "2014-06-17 10:28:27.0",
"piStatus": 0,
"piUpdateDate": "2014-06-16 14:00:46.0"
},
{
"pExtractPointFlg": 0,
"piCreateDate": "2014-06-16 14:00:46.0",
"piCreaterId": 1,
"piDeleteFlg": 0,
"piEndTime": "2014-06-17 10:28:27.0",
"piEventId": 2,
"piExtractNewFlg": 0,
"piId": "c90a18391acb4f53b55ff176d0041c81",
"piLanguage": 1,
"piPptName": "dddd",
"piPresentationName": "555555555",
"piPresentorMail": "wuxu@test.com",
"piPresentorName": "wuxu_DDDD",
"piShareFlag": 0,
"piSharePDFName": "",
"piStartTime": "2014-06-17 10:28:27.0",
"piStatus": 0,
"piUpdateDate": "2014-06-16 14:00:46.0"
},
{
"pExtractPointFlg": 0,
"piCreateDate": "2014-06-16 09:41:18.0",
"piCreaterId": 1,
"piDeleteFlg": 0,
"piEndTime": "2014-06-17 10:28:27.0",
"piEventId": 2,
"piExtractNewFlg": 0,
"piId": "f11de1ab83854f7192facabd45f90ba8",
"piLanguage": 1,
"piPptName": "dddd",
"piPresentationName": "777777777",
"piPresentorMail": "wuxu@test.com",
"piPresentorName": "wuxu_DDDD",
"piShareFlag": 0,
"piSharePDFName": "",
"piStartTime": "2014-06-17 10:28:27.0",
"piStatus": 0,
"piUpdateDate": "2014-06-16 09:41:18.0"
},
{
"pExtractPointFlg": 0,
"piCreateDate": "2014-06-16 14:00:44.0",
"piCreaterId": 1,
"piDeleteFlg": 0,
"piEndTime": "2014-06-17 10:28:27.0",
"piEventId": 2,
"piExtractNewFlg": 0,
"piId": "f884e105bbf641e3a6a0686d4fafe439",
"piLanguage": 1,
"piPptName": "dddd",
"piPresentationName": "888888888",
"piPresentorMail": "wuxu@test.com",
"piPresentorName": "wuxu_DDDD",
"piShareFlag": 0,
"piSharePDFName": "",
"piStartTime": "2014-06-17 10:28:27.0",
"piStatus": 0,
"piUpdateDate": "2014-06-16 14:00:44.0"
}
]
}
]
}

二线中的嗷嗷者的主页 二线中的嗷嗷者 | 初学一级 | 园豆:14
提问于:2014-06-27 10:30
< >
分享
最佳答案
0

1.首先创建这样的一个 类:EventInfo类中包含泛型集合为Presentation 的 属性

2.再使用相关的工具反序列化出来。Newtonsoft.Json.JavaScriptConvert.DeserializeObject<EventInfo>('{...}');

( Newtonsoft.Json.dll工具,网上可以download)

收获园豆:5
Vincent_void | 菜鸟二级 |园豆:238 | 2014-06-27 10:39

找不到 这 个 JavaScriptConvert 类 / 命名空间啊

二线中的嗷嗷者 | 园豆:14 (初学一级) | 2014-06-27 14:57

这是我的笨方法 

 

 

            JObject b = JObject.Parse(strResult);
            string strEvent = b.SelectToken("eventInfo").ToString();
            JArray jEventArr = JArray.Parse(strEvent);
            List<EventInfoModel> listEvent = new List<EventInfoModel>();
            foreach (JObject jEventObj in jEventArr)
            {
                EventInfoModel eventInfo = new EventInfoModel()
                {
                    CreateDate = Convert.ToDateTime(jEventObj["eiCreateDate"].ToString()),
                    CreaterId = jEventObj["eiCreaterId"].ToString(),
                    DeleteFlag = jEventObj["eiDeleteFlg"].ToString(),
                    EndTime = Convert.ToDateTime(jEventObj["eiEndTime"].ToString()),
                    EventId = jEventObj["eiId"].ToString(),
                    EventName = jEventObj["eiEventName"].ToString(),
                    EventTypeId = jEventObj["eiEventTypeId"].ToString(),
                    PreCollection = new ObservableCollection<PresentationModel>(),
                    StartTime = Convert.ToDateTime(jEventObj["eiStartTime"].ToString()),
                    UpdateDate = Convert.ToDateTime(jEventObj["eiUpdateDate"].ToString())
                };

                listEvent.Add(eventInfo);
                string strPre = jEventObj.SelectToken("presentation").ToString();

                JArray jPreArr = JArray.Parse(strPre);
                foreach (JObject jPreObj in jPreArr)
                {
                    PresentationModel presentation = new PresentationModel();

                    presentation.CreateDate = Convert.ToDateTime(jPreObj["piCreateDate"].ToString());
                    presentation.CreaterId = jPreObj["piCreaterId"].ToString();
                    presentation.DeleteFlag = jPreObj["piDeleteFlg"].ToString();
                    presentation.EndTime = Convert.ToDateTime(jPreObj["piEndTime"].ToString());
                    presentation.EventId = jPreObj["piEventId"].ToString();
                    presentation.ExtractNewFlag = jPreObj["piExtractNewFlg"].ToString();
                    presentation.ExtractPointFlag = jPreObj["pExtractPointFlg"].ToString();
                    presentation.Language = jPreObj["piLanguage"].ToString();
                    presentation.PptName = jPreObj["piPptName"].ToString();
                    presentation.PreId = jPreObj["piId"].ToString();
                    presentation.PresentationName = jPreObj["piPresentationName"].ToString();
                    presentation.PresentatorMail = jPreObj["piPresentorMail"].ToString();
                    presentation.PresentatorName = jPreObj["piPresentorName"].ToString();
                    presentation.ShareFlag = jPreObj["piShareFlag"].ToString();
                    presentation.SharePdfName = jPreObj["piSharePDFName"].ToString();
                    presentation.StartTime = Convert.ToDateTime(jPreObj["piStartTime"].ToString());
                    presentation.Status = jPreObj["piStatus"].ToString();
                    presentation.UpdateDate = Convert.ToDateTime(jPreObj["piUpdateDate"].ToString());

                    eventInfo.PreCollection.Add(presentation);
                }

            }

 

二线中的嗷嗷者 | 园豆:14 (初学一级) | 2014-06-27 14:58
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册