转换来自 web 服务的 Json 格式响应
Posted
技术标签:
【中文标题】转换来自 web 服务的 Json 格式响应【英文标题】:Convert Json format Response from webservice 【发布时间】:2013-08-08 11:32:41 【问题描述】:我的 web 服务返回 json 数据,如下所示,但我想要的就像在 2nd CodeSnipts 中一样。我的网络服务和课程如下所示。
"ShowID": 10107,
"StartTime": "3:00 PM",
"MovieID": 13,
"Movie": "Bhaag Milkha Bhaag ",
"Screen": "CDC SCreen2",
"MediaPath": "bmb1_568962.jpg"
, "ShowID": 115,
"StartTime": "6:00 PM",
"MovieID": 13,
"Movie": "Bhaag Milkha Bhaag ",
"Screen": "CDC SCreen2",
"MediaPath": "bmb1_568962.jpg"
, "ShowID": 110,
"StartTime": "9:00 PM",
"MovieID": 13,
"Movie": "Bhaag Milkha Bhaag ",
"Screen": "CDC SCreen2",
"MediaPath": "bmb1_568962.jpg"
但我想像
"MovieID": 13,
"Movie": "Bhaag Milkha Bhaag ",
"Screen": "CDC SCreen2",
"MediaPath": "bmb1_568962.jpg",
"ShowInfo": [
"ShowID": 10107,
"StartTime": "3:00 PM"
,
"ShowID": 115,
"StartTime": "6:00 PM"
,
"ShowID": 110,
"StartTime": "9:00 PM"
]
我的 Webservice 的 c# 代码
[WebMethod]
public string NowShowingGetList(DateTime ShowDate)
HomeController obj = new HomeController();
javascriptSerializer js = new JavaScriptSerializer();
string retJSON = js.Serialize(obj.NowShowingGetList(ShowDate));
return retJSON;
分类为
public class NowShowingInfo
public int ShowID get; set;
public string StartTime get; set;
public int MovieID get; set;
public string Movie get; set;
public string Screen get; set;
public string MediaPath get; set;
这里obj.NowShowingGetList(ShowDate)
返回列表
提前谢谢你。
【问题讨论】:
你需要创建一个新的类名 ShowInfo 并在 NowShowingInfo 中添加它的 Array 或 List 。 ShowInfo 类将包含属性名称:ShowId 和 StartTime 你需要创建一个新的类名 ShowInfo 并在 NowShowingInfo 中添加它的 Array 或 List 。 ShowInfo 类将包含 【参考方案1】:根据实体进行更改
public class NowShowingInfo
public List<ShowInfo> ShowInformation get; set;
public int MovieID get; set;
public string Movie get; set;
public string Screen get; set;
public string MediaPath get; set;
public class ShowInfo
public int ShowID get; set;
public string StartTime get; set;
在您的网络服务中进行相关更改
【讨论】:
如何将我的 NowShowingInfo 转换为您的 NowShowingInfo obj.NowShowingGetList(ShowDate) 返回我的类型的 NowShowingInfo 新手意味着@NisargShah 好的。首先,您的 obj.NowShowingGetList(ShowDate) 现在将为 1 部电影返回 1 个值。 NowshowingInfo 的对象将只包含 1 部电影的一个值。并且演出时间将存储在列表中。 IE。 ShowInformation.Add(ShowInfoObject)【参考方案2】:信息如下
public class ShowInfo
public int ShowID get; set;
public string StartTime get; set;
public class NowShowing
public List<ShowInfo> ShowInfo get; set;
public int MovieID get; set;
public string Movie get; set;
public string Screen get; set;
public string MediaPath get; set;
和
public List<NowShowing> NowShowingGetList(DateTime ShowDate)
List<NowShowingInfo> objshowList = obj.NowShowingGetList(ShowDate);
int movieID = 0;
List<NowShowing> objShowingList = new List<NowShowing>();
NowShowing obj2 = new NowShowing();
ShowInfo objshowInfo = new ShowInfo();
List<ShowInfo> objshowInfoList = new List<ShowInfo>();
int count = 0;
string Screen="";
foreach (NowShowingInfo objs in objshowList)
if (movieID != objs.MovieID )
if (count != 0)
obj2.ShowInfo = objshowInfoList;
objshowInfoList = new List<ShowInfo>();
objShowingList.Add(obj2);
count = 0;
obj2 = new NowShowing();
obj2.MovieID = objs.MovieID;
obj2.Movie = objs.Movie;
obj2.Screen = objs.Screen;
obj2.MediaPath = objs.MediaPath;
if (count == 0)
objshowInfo = new ShowInfo();
objshowInfo.ShowID = objs.ShowID;
objshowInfo.StartTime = objs.StartTime;
objshowInfoList.Add(objshowInfo);
else
objshowInfo = new ShowInfo();
objshowInfo.ShowID = objs.ShowID;
objshowInfo.StartTime = objs.StartTime;
objshowInfoList.Add(objshowInfo);
movieID = objs.MovieID;
Screen = objs.Screen;
count++;
obj2.ShowInfo = objshowInfoList;
objShowingList.Add(obj2);
return objShowingList;
【讨论】:
以上是关于转换来自 web 服务的 Json 格式响应的主要内容,如果未能解决你的问题,请参考以下文章