c# - How to return JSON array in container object -



c# - How to return JSON array in container object -

i have next asp.net 4.5 web api method:

public ienumerable<rcwindswcfservice.uspgetallstationsresult> getextallstations() { rcwindswcfservice.service1client client = new rcwindswcfservice.service1client(); ienumerable<rcwindswcfservice.uspgetallstationsresult> results = client.getextallstations(); homecoming results; }

this returns next json results:

[{point_x: -81.0410610591,point_y: 34.1831858023,table_name: "cedar creek"},{point_x: -80.7653777161,point_y: 33.8641198907,table_name: "gadsden"}]

what need homecoming is:

{station_coordinates: [{point_x: -81.0410610591,point_y: 34.1831858023,table_name: "cedar creek"},{point_x: -80.7653777161,point_y: 33.8641198907,table_name: "gadsden"}]}

as new these technologies, appreciate suggestions how achieved. if possible, specific recommended changes above code helpful.

thank you

to improve formatting in response @l.b comment below re-posting it:

public ienumerable<rcwindswcfservice.uspgetallstationsresult> getextallstations() { rcwindswcfservice.service1client client = new rcwindswcfservice.service1client(); stationcoordinates coordinates = new stationcoordinates(); ienumerable<rcwindswcfservice.uspgetallstationsresult> results = client.getextallstations(); coordinates.station_coordinates = results; homecoming coordinates; } class stationcoordinates { public ienumerable<rcwindswcfservice.uspgetallstationsresult> station_coordinates { set; get; } }

i receiving: cannot implicitly convert type 'rcwindsextsvc.controllers.rcwindsextsvccontroller.stationcoordinates' 'system.collections.generic.ienumerable'. explicit conversion exists (are missing cast?) - suspect improper usage on part.

thank 1 time again in advance.

update:

the code has been modified reflect moby disk's comment below:

public ienumerable<stationcoordinates> getextallstations() { rcwindswcfservice.service1client client = new rcwindswcfservice.service1client(); stationcoordinates coordinates = new stationcoordinates(); //ienumerable<coordinates> results = client.getextallstations(); ienumerable<rcwindswcfservice.uspgetallstationsresult> results = client.getextallstations(); coordinates.station_coordinates = results; homecoming coordinates; } class stationcoordinates { public ienumerable<rcwindswcfservice.uspgetallstationsresult> station_coordinates { set; get; } }

the error code remains:

cannot implicitly convert type 'rcwindsextsvc.controllers.rcwindsextsvccontroller.stationcoordinates' 'system.collections.generic.ienumerable'. explicit conversion exists (are missing cast?)

l.b.'s reply seems work fine me:

using system; using system.collections.generic; using system.linq; using system.net; using system.net.http; using system.web.http; namespace server.controllers { public class tempcontroller : apicontroller { public stationcoordinates getextallstations() { rcwindswcfservice.service1client client = new rcwindswcfservice.service1client(); stationcoordinates coordinates = new stationcoordinates(); ienumerable<rcwindswcfservice.uspgetallstationsresult> results = client.getextallstations(); coordinates.station_coordinates = results; homecoming coordinates; } public class stationcoordinates { public ienumerable<rcwindswcfservice.uspgetallstationsresult> station_coordinates { set; get; } } } } namespace rcwindswcfservice { public class uspgetallstationsresult { public double point_x { get; set; } public double point_y { get; set; } public string table_name { get; set; } } public class service1client { public ienumerable<uspgetallstationsresult> getextallstations() { homecoming new list<uspgetallstationsresult>() { new uspgetallstationsresult() {point_x= -81.0410610591,point_y= 34.1831858023,table_name= "cedar creek"}, new uspgetallstationsresult() {point_x= -80.7653777161,point_y= 33.8641198907,table_name= "gadsden"} }; } } }

the json output above is:

{"station_coordinates":[{"point_x":-81.0410610591,"point_y":34.1831858023,"table_name":"cedar creek"},{"point_x":-80.7653777161,"point_y":33.8641198907,"table_name":"gadsden"}]}

which believe wanted.

c# json web-services

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

java - Parsing XML, skip certain tags -

c# - ASP.NET MVC Sequence contains no matching element -