LINQ to JSON in .net 4.0 Client Profile

How will I use LINQ for JSON in a .net 4 client profile in C #. I would like to request some json answers as shown in the msdn blog post without writing out a contract (datacontract, servicecontract, etc.). I really only need to request (read) the response, I do not need to modify the json response. (Would datacontract also be faster than LINQ for JSON?)

Alternatively, I could use XML or the full .net 4 framework, but I hope this can be avoided. I can use external libraries if this is better than installing the whole structure.

+4
source share
2 answers

JSON.net is pretty popular and it seems you like it.

+3
source

If your service is used by Ajax clients (jQuery), you will get better performance using JSON.

Another recommendation; To get rid of the same domain policy, I recommend that you enable the crossDomainScriptAccessEnabled function:

<webHttpBinding> <binding name="myHttpBinding" crossDomainScriptAccessEnabled="true" /> </webHttpBinding> 

Regarding DataContract; DataContract is not needed in your script.

Code example:

  • Your service:

     [ServiceContract] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class BlogService { [OperationContract] [WebGet(ResponseFormat = WebMessageFormat.Json)] public List<Article> GetBlogArticles() { return Article.GetDummyArticles.ToList(); } } 
  • Article class (excerpt):

     public class Article { public string Title { get; set; } public string Body { get; set; } public static IEnumerable<Article> GetDummyArticles() { yield return new Article { Title = "Article 1", Body = "sdlkfjsdlkfjskl" }; yield return new Article { Title = "Article 2", Body = "sfsfsdfd23434wfdfsfdfkfjskl" }; } } 

For your scenario, I actually cannot find a reason to use any (third-party) library, since WCF4 already contains native JSON support with or without Padding.

+1
source

All Articles