0

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
    }, {...}
}

2 Answers 2

3

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 );
Sign up to request clarification or add additional context in comments.

6 Comments

this looks great. Let me try this solution.
your solution, is it similar to this one? stackoverflow.com/questions/21657122/…
@andrey.shedko No, i saw the link you posted its a completely different question and answer. Did this solution work for you ?
Did not try yet. I will let you know.
jsonInput should be result of the LINQ query (var result), right?
|
2

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.

3 Comments

Claudio, my background based on vb.net and linq, so I will be appreciate a lot if you can show any example of that.
@andrey.shedko: check the structure of the json you need to return an translate that to a C# class structure. If you have troubles with that come again and I'll be glad to help.
To say honestly, yes, I have troubles with that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.