Reputation: 683
Using MVC4 c# environment. I am needing to get a namspace to my jsonObject. Currently I am using the following structure.
var results =
Globals.StaticList
.Where(x => x.LookupName.ToLower().Contains(searchString.ToLower()))
.Select(x => new nvp()
{
Name = x.LookupName,
ID = x.LookupName
}).Distinct().ToArray();
return new JsonResult() { Data = results.Take(10), JsonRequestBehavior = JsonRequestBehavior.AllowGet };
which returns like this:
[{"Name":"Name 1","ID":"Name 1"},{"Name":"Name 2","ID":"Name 2"}]
The issue is I would like for it to return as
{"TheNameSpace": [{"Name":"Name 1","ID":"Name 1"},{"Name":"Name 2","ID":"Name 2"}]}
my nvp class looks like this:
[DataContract(Namespace = "TheNameSpace")]
public class nvp
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string ID { get; set; }
}
I have tried a few different variations of DataContractSerialization like this.. but it fails miserably:
var ms = new MemoryStream();
var ser = new DataContractSerializer(typeof (nvp[]));
ser.WriteObject(ms,results);
var streamreader = new StreamReader(ms);
return streamreader.ReadToEnd();
Any help is appreciated. Thanks
Upvotes: 0
Views: 939
Reputation: 1166
try :
var results =
Globals.StaticList
.Where(x => x.LookupName.ToLower().Contains(searchString.ToLower()))
.Select(x => new nvp()
{
Name = x.LookupName,
ID = x.LookupName
}).Distinct().ToArray().Take(10);
return new Json(new {TheNameSpace= results},JsonRequestBehavior = JsonRequestBehavior.AllowGet);
Hope will help.
Upvotes: 1