4 Replies - 1517 Views - Last Post: 04 February 2017 - 04:03 PM

#1 fearfulsc2  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 169
  • Joined: 25-May 16

Entity Framework to convert into Json Object

Posted 19 January 2017 - 12:34 PM

Hey everyone, I'm trying to mess around with JSON and Entity Framework together in a data layer.

I know that when I created a SOAP service using WCF, XML worked very well, but now I want to be able to toy around with JSON

So if I were to do write something like this before how I normally did it
    public List<State> GetStates()
    {
      return ed.States.ToList();
    }



How would I be able to return it so that it does a JSON object instead? Any tips/advice on that or any resources I can find? It appears that everything I find is more related to MVC and web API

Is This A Good Question/Topic? 0
  • +

Replies To: Entity Framework to convert into Json Object

#2 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6376
  • View blog
  • Posts: 25,763
  • Joined: 12-December 12

Re: Entity Framework to convert into Json Object

Posted 20 January 2017 - 02:39 AM

Hello. I don't know much about this but I explored a little, out of interest.

What type of application are you building? There is more information about this with ASP and WCF because the data is sent across a wire so it is a more common requirement. ASP (.NET MVC) has a method like JsonResult (and I suspect WCF can more or else handle JSON automatically).

Otherwise, you'll need to explore JSON serialization perhaps using Json.NET. With Json.NET you can write something like this:

string json = JsonConvert.SerializeObject(product);

[I noticed in an SO answer someone used return Json(data);. This looked promising but then I've no idea where he magic-ed that method from. A dead-end.]

If this is for a desktop application, though, then there might be little point, as all you would end up doing with the serialized data is to immediately deserialize it. (Even if you serialize and deserialize a memory stream you haven't gained much because it is not going across a wire. That is, a data-interchange format is not required.)

I would probably let EF return standard POCO objects and the client can then serialize the data if it is required to be persisted. [It is already persisted though, it is in the database ;)]



Off-topic: I was asked recently what JSON stands for and I replied 'Javascript Symbolic Object Notation'. I think my version is sexier (but wrong).
Was This Post Helpful? 0
  • +
  • -

#3 fearfulsc2  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 169
  • Joined: 25-May 16

Re: Entity Framework to convert into Json Object

Posted 20 January 2017 - 08:36 AM

This will be a web application that will have mobile functionality in the future. My technical lead wants us to us JSON, so I am trying to learn how to use JSON with Entity Framework and SOAP. He doesn't like XML. I know that there is a JsonResult for MVC. Can I convert from XML to JSON directly or would I have to convert it into an object and then convert to Json?
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6376
  • View blog
  • Posts: 25,763
  • Joined: 12-December 12

Re: Entity Framework to convert into Json Object

Posted 20 January 2017 - 08:47 AM

I'm sure it's possible to convert XML to JSON but,considering they are alternative formats,I wouldn't pursue that route initially.
Was This Post Helpful? 0
  • +
  • -

#5 wtp  Icon User is offline

  • D.I.C Regular

Reputation: 26
  • View blog
  • Posts: 319
  • Joined: 08-December 11

Re: Entity Framework to convert into Json Object

Posted 04 February 2017 - 04:03 PM

I think you can switch your WCF service to reply as JSON by default

"If you want nice json without hardcoding attributes into your service classes,
use
 <webHttp defaultOutgoingResponseFormat="Json"/>
in your behavior config"
http://stackoverflow...m-a-wcf-service

This post has been edited by wtp: 04 February 2017 - 04:04 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1