andrey.shedko
andrey.shedko

Reputation: 3238

How make web api return json in needed format?

I'm using following code in web api to return json:

So, here is dto class. But it's not clear how to map it to result.

public class grafikresult
{
    int kodg { get; set; }
    string name { get; set; }
    DateTime data { get; set; }
    byte pax { get; set; }
    byte ch { get; set; }
}  

Here is controller code:

// GET api/Grafik/5
    public IHttpActionResult GetGrafik(int id)
    {
        PhuketEntities db = new PhuketEntities();
        xTourist t = db.xTourist.Find(id);
        var result = from a in db.Grafik
                     join b in db.Exc on a.Excursion equals b.Kod
                     join c in db.Dates on a.Kodd equals c.kodd
                     join d in db.Staff on a.Guide equals d.Kod
                     where c.Date > t.ArrDate && c.Дата < t.DepDate
                     select new { kodg = a.Kodg, name = d.Name, data = c.Date, pax = t.Pax, ch = t.Child };
        return Ok(result);
    }

This returns new json like that:

{
    "$id": "1",
    "$values": [{
        "$id": "2",
        "kodg": -1643387437,
        "name": null,
        "data": "2014-02-07T00:00:00",
        "pax": 2,
        "ch": 0
    }, {...}]
}

How should I change web api controller code to be able get this json:

{
    "$id": "1",
    "kodg": -1643387437,
    "name": null,
    "data": "2014-02-07T00:00:00",
    "pax": 2,
    "ch": 0
    }, {...}
}

Upvotes: 0

Views: 893

Answers (2)

Yasser Shaikh
Yasser Shaikh

Reputation: 47804

You could try using AutoMapper.

1. Create classes for your Json

Before you use that you WILL need to convert your JSON object to C# classes. Try this or this.

In case you are having trouble defining classes, here is what I think should work for you.

Class for first json.

public class JsonClassA
{
    int id{ get; set; }
    Details values { get; set; }
}  

public class Details
{
    int kodg { get; set; }
    string name { get; set; }
    DateTime data { get; set; }
    byte pax { get; set; }
    byte ch { get; set; }
} 

Class for second json.

public class JSONClassB
{
    int kodg { get; set; }
    string name { get; set; }
    DateTime data { get; set; }
    byte pax { get; set; }
    byte ch { get; set; }
} 

2. Deserialize using JavaScriptSerialize:

JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
JsonClassA jsonClassA = jsonSerializer.Deserialize<JsonClassA>(jsonInput)

3. Map using Automapper

Say your first Json represent class JsonClassA and your second JSON represents class JSONClassB.

Using Automapper this could easily be done as follows and be returned from your controller.

JSONClassB finalOutput = AutoMapper.CreateMap<JsonClassA, JSONClassB>()
      .ForMember(dest => dest.prop1, opts => opts.MapFrom(src => src.propAAA))
      .ForMember(dest => dest.prop2, opts => opts.MapFrom(src => src.propBBB));
return Ok(finalOutput );

Upvotes: 3

Claudio Redi
Claudio Redi

Reputation: 68440

Create a DTO class having the exact structure you need to return. Then you would map the structure of the items on result to PhuketDTO and assign the DTO list to Ok as you currently do for result.

Upvotes: 2

Related Questions