Domanda

I have two classes in WCF Service:

[DataContract]
public class item
{
    [DataMember]
    public string categoryid
    {
        get;
        set;
    }
    [DataMember]
    public string title
    {
        get;
        set;
    }
}
[DataContract]
public class Employee
{
    [DataMember]
    public string Id
    {
        get;
        set;
    }
    [DataMember]
    public string Name
    {
        get;
        set;
    }       
}

I am fetching data from DataSets using LINQ:

public List<Employee> GetCities()
{ 
    var em = ((from DataRow dr in ds.Tables["City"].Rows
        select new
        {
            Id = dr["intCityId"].ToString(),
            Name = dr["strTitle"].ToString()                          
        }).Select(x => new Employee() { Id = x.Id, Name = x.Name}).ToList());
}

Now I'm getting JSON data by return this value. Also I've got values from the item class also but now I want to convert the list data into JSON format just like following::

{"content":{"em" :[{ "id" : "1","Name" : "name"},{ "id" : "2","Name" : "name2"}],
"item":[{"category":"Sports","Title":"Football"},{"category":"Sports1","Title":"Football2"}]}}

Meaning I want to merge these two classes result as one but in above format which is clean JSON format

Please Help...

È stato utile?

Soluzione

You can also serialize anonymous objects to JSON.

Example:

var cities  = GetCities();
var employees = GetEmployees();
return new JsonResult { Data = new { Content = new { Employees = employees, Cities = cities } } };

Altri suggerimenti

Define a new object content that contains a list of employee and a list of item. Populate the object and serialize it via JSON serializer.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top