c# - give names to a var object in .net -
just wondering using web method return json data web form. anyway don't want entire class in json 2 of columns. used linq columns here example.
ienumerable<person> model = new list<person> { new person { id = 1, name = "bryan", phone = "218-0211", email = "bryan@mail.mil" }, new person { id = 2, name = "joe", phone = "248-0241", email = "joe@mail.mil" }, new person { id = 3, name = "fred", phone = "354-0441", email = "fred@mail.mil" }, new person { id = 4, name = "mary", phone = "344-3451", email = "mary@mail.mil" }, new person { id = 5, name = "jill", phone = "127-3451", email = "jill@mail.mil" } }; var mysubset = in model select new { a. name, a.email };
unfotunately when serialize result , send lose column names. data.name doesn't work wondering can give names var type? example there way this?
var mysubset = in model select new { a. name, a.email }; string myname string; foreach (var item in mysubset) { myname = subset.name; }
ok here actual code sorry in vb project inherited
dim mycollection = in cmpnylist select {a.cmpnyname, a.shipfrom} return jsserialize.serialize(mycollection)
the json returned
[{"company a","new york"},{"company b", "harrisburg"}]
so i'm trying like
[{"cmpnyname":"company a", "shipfrom": "new york"}, {"cmpnyname": "company b", "shipfrom": "harrisburg}]
i believe you're using json.net answer using jobject(newtonsoft.json.linq)
jobject o = new jobject( new jproperty("personsslist", new jarray( p in model select new jobject( new jproperty("personname", p.name), new jproperty("email", p.email)))));
and result json
{ "personsslist": [ { "personname": "bryan", "email": "bryan@mail.mil" }, { "personname": "joe", "email": "joe@mail.mil" }, { "personname": "fred", "email": "fred@mail.mil" }, { "personname": "mary", "email": "mary@mail.mil" }, { "personname": "jill", "email": "jill@mail.mil" } ]
Comments
Post a Comment