c# - how to show a json file in JSON format -
i not able see data in json format below. although have get/set don't know why or how data show in format!
desired format:
{ "cinemasf" : [ { "customer_id" : "customer0001", "ticket" : { "ticket_id" : "1", "round" : { "idticket" : "2015-01-01 15:44:40", "pieces" : "13.00", "rate" : "100.00" }, "down" : { "idticket" : "2015-01-01 21:04:40", "pieces" : "13.00", "rate" : "100.00" } }, "dur_info" : { "start" : "102.34", "end" : "90.34", "climax" : "120.50" }, "cine_event" : { "score" : "80.5", "climax_event" : [ { "event_id" : "0", "idticket" : "2015-01-01 15:44:40", "pieces" : "13.00", "rate" : "100.00" }, { "event_id" : "0", "idticket" : "2015-01-01 15:44:40", "pieces" : "13.00", "rate" : "100.00" } } } ] }
instead getting format:
public class cinemasf { public string customer_id { get; set; } public list<ticket> ticket_cinemasf { get; set; } public list<dur_info> dur_info_cinemasf { get; set; } public list<driving_event> driving_event_cinemasf { get; set; } } public class ticket { public int ticket_id { get; set; } public list<pieces> pieces_cinemasf { get; set; } public list<rate> rate_cinemasf { get; set; } } public class round { public string idticket { get; set; } public string pieces { get; set; } public string rate { get; set; } } public class down { public string idticket { get; set; } public string pieces { get; set; } public string rate { get; set; } } public class dur_info { public string start { get; set; } public string end { get; set; } public string climax { get; set; } } public class cine_event { public string score { get; set; } public list<climax_event> climax_event_cinemasf { get; set; } } public class climax_event { public string event_id { get; set; } public string idticket { get; set; } public string pieces { get; set; } public string rate { get; set; } }
i using following code retrieve data:
string url = "http://xxxxxxx.php"; var client_cinemasf = new restclient(url); var request_cinemasf = new restrequest(method.get); irestresponse response_cinemasf = client_cinemasf.execute(request_cinemasf); var content_cinemasf = response_cinemasf.content; cinemasf root_cinemasf = new cinemasf(); cinemasf deserializeplayer_cinemasf = jsonconvert.deserializeobject<cinemasf>(content_cinemasf.tostring()); console.writeline(deserializeplayer_cinemasf.trip_cinemasf[0]);
why can't show deserializeplayer_cinemasf.trip_cinemasf[0]
there few issues found
json malformed should this
{ "cinemasf": [ { "customer_id": "customer0001", "ticket": { "ticket_id": "1", "round": { "idticket": "2015-01-01 15:44:40", "pieces": "13.00", "rate": "100.00" }, "down": { "idticket": "2015-01-01 21:04:40", "pieces": "13.00", "rate": "100.00" } }, "dur_info": { "start": "102.34", "end": "90.34", "climax": "120.50" }, "cine_event": { "score": "80.5", "climax_event": [ { "event_id": "0", "idticket": "2015-01-01 15:44:40", "pieces": "13.00", "rate": "100.00" }, { "event_id": "0", "idticket": "2015-01-01 15:44:40", "pieces": "13.00", "rate": "100.00" } ] } } ] }
next need understand hierarchy of elements inside it
you need rootelement json desearlize should
public class rootjson { [jsonproperty("cinemasf")] public cinemasf csf { get; set; } } public class cinemasf { [jsonproperty("customer_id")] public string customer_id { get; set; } [jsonproperty("ticket")] public list<ticket> ticket_cinemasf { get; set; } [jsonproperty("dur_info")] public list<dur_info> dur_info_cinemasf { get; set; } [jsonproperty("cine_event")] public list<driving_event> driving_event_cinemasf { get; set; } }
now come last part
class ticket
must have
[jsonproperty("ticket_id")] [jsonproperty("round")] [jsonproperty("down")]
and round
, down
can have
public list<pieces> public list<rate>
then @ last can go for
rootjson deserializeplayer_cinemasf = jsonconvert.deserializeobject<rootjson>(content_cinemasf.tostring());
Comments
Post a Comment