我试图将一些json dezilirze到我的班级我使用http://json2csharp.com/处的工具来转换来自以下的json请求
{
"movie_details": {
"id": "1",
"name": "movie 1",
"description": "movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1\r\n\r\nmovie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1\r\n\r\nmovie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1\r\n\r\nmovie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1\r\n\r\nmovie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1\r\n\r\nmovie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1movie 1\r\n",
"image": "http://popcorn.pk/img/Movie/harley3.jpg",
"secondry_images": "movie/Jellyfish.jpg,movie/Koala.jpg,movie/Jellyfish.jpg",
"actor": "anandan",
"actoress": "anandan",
"director": "anandan",
"music_director": "anandan",
"release_date": "March 18, 2015",
"tags": "movie 1,movie 1 ",
"age_restriction": "14",
"box_office": "36985214",
"official_site": "http://google.com",
"duration": "1.50 hr",
"writers": "anandan",
"imdb": 8,
"status": "1",
"language": "Tamil",
"created": "2015-03-11 05:11:31",
"modified": "2015-03-25 10:12:39",
"movie_category_id": "Action",
"slug": "movie 1 ",
"movie_show_time_id": "0",
"theatre_movie_screen_id": "0",
"videous": "",
"videos": "",
"comming_soon": "0",
"avg": 0,
"movie_details_url": "http://popcorn.pk/launch/Movies/view/1"
},
"fetch_timing": [
{
"id": "23",
"theater_name": "SUPER CINEMA - ROYAL PALM",
"address": "Royal Palm Golf and Country Club, 52 Canal Bank road, Lahore",
"show_times": [
"4:00 pm"
],
"screen_id": "28",
"title": "Standard Screen"
},
{
"id": "24",
"theater_name": "SOZO WORLD CINEMA",
"address": "Fortress Stadium, Lahore - Cantt, Lahore, Pakistan",
"show_times": [
"2:30 pm",
"7:30 pm"
],
"screen_id": "29",
"title": "Standard Screen"
},
{
"id": "25",
"theater_name": "ATRIUM CINEMA",
"address": "3rd Floor, Atrium Mall, 249 Staff Lines, Zaibunnisa Street, Saddar. Karachi, Pakistan",
"show_times": [
"5:15 pm",
"11:30 am"
],
"screen_id": "30",
"title": "Standard Screen"
},
{
"id": "26",
"theater_name": "UNIVERSE CINEPLEX",
"address": "55-C, Khayaban-e-Muslim, Muslim Commercial Area Phase 6, D.H.A. Karachi, Pakistan",
"show_times": [
"3:30 pm",
"9:30 pm",
"6:30 pm"
],
"screen_id": "31",
"title": "Standard Screen"
},
{
"id": "27",
"theater_name": "CINEPAX - OCEAN MALL",
"address": "Cinepax, Ocean Tower, 2 Sword, Clifton, Karachi",
"show_times": [
"5:45 pm"
],
"screen_id": "32",
"title": "Standard Screen"
},
{
"id": "28",
"theater_name": "CINEPAX - CITY AUDITORIUM",
"address": "Opposite Meezan Bank Regional Office at Ayesha Manzil, FB Area Block 14,Karachi,Pakistan",
"show_times": [
"3:00 pm"
],
"screen_id": "33",
"title": "Standard Screen"
},
{
"id": "29",
"theater_name": "NUEPLEX",
"address": "The Place, Khayaban e Shaheen Phase 8, D.H.A",
"show_times": [
"6:15 pm",
"3:10 pm"
],
"screen_id": "34",
"title": "Standard Screen"
},
{
"id": "30",
"theater_name": "CENTAURUS CINEPLEX",
"address": "Centaurus Mall, 1 Jinnah Avenue, 4th Floor Centaurus, F-8 Islamabad",
"show_times": [
"8:00 pm"
],
"screen_id": "35",
"title": "Standard Screen"
},
{
"id": "31",
"theater_name": "THE ARENA",
"address": "The Arena, Rawapindi, Pakistan",
"show_times": [
"8:30 pm",
"5:45 pm"
],
"screen_id": "36",
"title": "Standard Screen"
},
{
"id": "32",
"theater_name": "CINE MOOSH",
"address": "House no. A/18, Unit no.3, Main AutoBhan Road, Latifabad, Hyderabad",
"show_times": [
"3:00 pm"
],
"screen_id": "37",
"title": "Standard Screen"
},
{
"id": "33",
"theater_name": "CINEGOLD PLEX",
"address": "Main Boulevard, Garden City, between Phase 7 & 8, Expressway, Rawalpindi 46000",
"show_times": [
"7:45 pm"
],
"screen_id": "38",
"title": "Standard Screen"
},
{
"id": "34",
"theater_name": "CINEPAX JINNAH PARK",
"address": "Cinepax Jinnah Park Kachehri Road, Rawalpindi 00e220ac201c Pakistan",
"show_times": [
"12:00 pm",
"8:00 pm"
],
"screen_id": "39",
"title": "Standard Screen"
},
{
"id": "35",
"theater_name": "CINEPAX HOTEL ONE",
"address": "Hotel One 99/C, Peoples Colony (Near D Ground) Faisalabad",
"show_times": [
"3:00 pm"
],
"screen_id": "40",
"title": "Standard Screen"
},
{
"id": "36",
"theater_name": "SUPER CINEMA",
"address": "Vogue Tower, MM Alam road, Lahore",
"show_times": [
"2:15 pm"
],
"screen_id": "41",
"title": "Standard Screen"
},
{
"id": "38",
"theater_name": "CINE STAR",
"address": "Plot #13,2 civic center township Lahore",
"show_times": [
"7:15 pm"
],
"screen_id": "43",
"title": "Standard Screen"
},
{
"id": "39",
"theater_name": "TAJ CINEMA",
"address": "Plot #13,2 civic center township Lahore",
"show_times": [
"2:00 pm",
"10:00 pm"
],
"screen_id": "44",
"title": "Standard Screen"
},
{
"id": "40",
"theater_name": "CINESTAR IMAX",
"address": "Plot #13,2 civic center township Lahore",
"show_times": [
"2:00 pm",
"7:30 pm"
],
"screen_id": "45",
"title": "Standard Screen"
},
{
"id": "41",
"theater_name": "Taj Mahal Multiplex",
"address": "Taj Mahal Multiplex Cinemas, Railway Road, Faisalabad-38000",
"show_times": [
"4:00 pm",
"10:00 pm",
"7:00 pm"
],
"screen_id": "46",
"title": "Standard Screen"
},
{
"id": "42",
"theater_name": "The Arena Orchard",
"address": "Phase 1 Baharia Orchard Raiwind Road, Lahore, Pakistan",
"show_times": [
"11:15 pm",
"11:00 am"
],
"screen_id": "47",
"title": "Standard Screen"
},
{
"id": "44",
"theater_name": "DHA Cinema",
"address": "305-R Block, D.H.A, Lahore Pakistan",
"show_times": [
"5:00 pm",
"10:30 pm"
],
"screen_id": "49",
"title": "Standard Screen"
},
{
"id": "45",
"theater_name": "Cinepax Fortress",
"address": "2nd Floor, Fortress Square Shopping Mall, Lahore - Pakistan",
"show_times": [
"8:15 pm",
"10:45 pm",
"2:30 pm"
],
"screen_id": "50",
"title": "Standard Screen"
},
{
"id": "47",
"theater_name": "Imperial Cinemas",
"address": "Imperial Garden Homes, Lahore, Pakistan",
"show_times": [
"2:30 pm"
],
"screen_id": "52",
"title": "Standard Screen"
},
{
"id": "48",
"theater_name": "CINEPAX GUJRANWALA",
"address": "Prince Cinepax Main G T Road, Gujranwala - Pakistan",
"show_times": [
"8:45 pm",
"5:45 pm",
"6:00 pm"
],
"screen_id": "53",
"title": "Standard Screen"
},
{
"id": "49",
"theater_name": "Super Cinema - Gujrat",
"address": "Super Cinema Gujrat, Gt Road, Gujrat",
"show_times": [
"1:00 pm",
"10:00 pm",
"7:00 pm"
],
"screen_id": "54",
"title": "Standard Screen"
},
{
"id": "50",
"theater_name": "PAF Cinema",
"address": "Sarfaraz Rafiqui Road, Lahore, Pakistan",
"show_times": [
"3:00 pm",
"11:00 pm",
"2:00 am"
],
"screen_id": "55",
"title": "Standard Screen"
},
{
"id": "51",
"theater_name": "Capri Cinema",
"address": "Gazdarabad, Karachi, Pakistan, Karachi, Pakistan",
"show_times": [
"6:00 pm"
],
"screen_id": "56",
"title": "Standard Screen"
}
]
}
但由于某些原因,我在列表的过程中遇到了一个deubugger错误。我的GetMovieDetailAysnc工作正在为json字符串带回文本。
public async Task<List<MovieDetail>> GetMovieDetailsList(int movieId)
{
string jsonresult = await WCFRESTServiceCall("GET", "movie_details");
var list = jsonresult.Deserialize<MovieDetails>();
return list.movieDetaillist;
}
但是我的错误在deserilize方法上出现了电影细节的类如下
public class MovieDetails
{
public string id { get; set; }
public string name { get; set; }
public string description { get; set; }
public string image { get; set; }
public string secondry_images { get; set; }
public string actor { get; set; }
public string actoress { get; set; }
public string director { get; set; }
public string music_director { get; set; }
public string release_date { get; set; }
public string tags { get; set; }
public string age_restriction { get; set; }
public string box_office { get; set; }
public string official_site { get; set; }
public string duration { get; set; }
public string writers { get; set; }
public int imdb { get; set; }
public string status { get; set; }
public string language { get; set; }
public string created { get; set; }
public string modified { get; set; }
public string movie_category_id { get; set; }
public string slug { get; set; }
public string movie_show_time_id { get; set; }
public string theatre_movie_screen_id { get; set; }
public string videous { get; set; }
public string videos { get; set; }
public string comming_soon { get; set; }
public int avg { get; set; }
public string movie_details_url { get; set; }
}
public class FetchTiming
{
public string id { get; set; }
public string theater_name { get; set; }
public string address { get; set; }
public List<string> show_times { get; set; }
public string screen_id { get; set; }
public string title { get; set; }
}
public class MovieDetail
{
public MovieDetails movie_details { get; set; }
public List<FetchTiming> fetch_timing { get; set; }
}
}
我的问题是,由于数据有效,我可以解决这个错误。我的Deserlize方法如下
public static T Deserialize<T>(this string SerializedJSONString)
{
var stuff = JsonConvert.DeserializeObject<T>(SerializedJSONString);
return stuff;
}
修改笔记
对于Futher investaget,我得到了错误
附加信息:无法将当前JSON对象(例如{&#34; name&#34;:&#34; value&#34;})反序列化为类型System.Collections.Generic.List`1 [ popcornpk.DataModel.MovieDetails]&#39;因为该类型需要JSON数组(例如[1,2,3])才能正确反序列化。
答案 0 :(得分:0)
我认为问题是您将错误的类型传递给Deserialize
方法 - 而不是MovieDetail
,而是MovieDetails
(如果我正确理解您的WCF服务返回单个实例)对通用Task
结果使用错误的类型。因此,只需尝试使用以下代码(包括签名)更新GetMovieDetailsList(int movieId)
方法:
public async Task<MovieDetail> GetMovieDetailsList(int movieId)
{
string jsonresult = await WCFRESTServiceCall("GET", "movie_details");
var details = jsonresult.Deserialize<MovieDetail>();
return details;
}