none
Framework of C# for JSON files communication RRS feed

  • Question

  • My project just assigned is to write a program to send / receive JSON files to communicate thru. http with a face recognition server. I don't have any experience on such kind of program. Is there any toolkits in C# about sending/receiving of JSON files thru http and parse JSON files?
    Wednesday, May 22, 2019 9:40 AM

Answers

  • hmm, using JSON files over http sounds redundant.

    You should start looking into WebClient/WebRequest and Newtonsoft.JSON..

    When you want to search for it, use nuget in VS, it has a search option ;)

    Wednesday, May 22, 2019 11:28 AM
  • JSON with HTTP is generally referred to as a REST API. There are plenty of libraries available. In .NET itself HttpClient is the preferred option but WebClient can also be used. If you want third party support then RestClient or AutoRest can be used.

    None of these actually convert JSON though as REST is technically format agnostic. Most people use JSON.NET for JSON support but if you want to keep it framework only then DataContractSerializer(?) but this has some limitations. .NET Core 3 will ship with a full, built in JSON parser if you can wait that long. There are also other JSON parsers available that may work better for your needs.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, May 22, 2019 2:14 PM
    Moderator

All replies

  • hmm, using JSON files over http sounds redundant.

    You should start looking into WebClient/WebRequest and Newtonsoft.JSON..

    When you want to search for it, use nuget in VS, it has a search option ;)

    Wednesday, May 22, 2019 11:28 AM
  • You can use HTTPClient (the standard) to send/receive Json data with an ASP.NET WebAPI program. The face recognition software  is kind of not important in the exchange of Json data between two programs over HTTP, which are the client and the service programs.

    You can use the DTO pattern to exchange the Json data between the two programs, which is known by the WebAPI client and WebAPI service programs.

    https://en.wikipedia.org/wiki/Data_transfer_object

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-5

    https://www.tutorialsteacher.com/webapi/what-is-web-api

    The code example is the client code that can be hosted by a ASP.NET Web program or a Windows Desktop program.

    using System;
    using System.Collections.Generic;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Text;
    using Entities;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    
    namespace ProgMgmntCore2UserIdentity.WebApi
    {
        public class WebApi : IWebApi
        {
            #region ProjectApi
            
            public List<DtoProject> GetProjsByUserIdApi(string userid)
            {
                var dtoprojects = new List<DtoProject>();
    
                using (var client = new HttpClient())
                {
                    var uri = new Uri("http://progmgmntcore2api.com/api/project/GetProjsByUserId?userid=" + userid);
    
                    var response = client.GetAsync(uri).Result;
    
                    if (!response.IsSuccessStatusCode)
                        throw new Exception(response.ToString());
    
                    var responseContent = response.Content;
                    var responseString = responseContent.ReadAsStringAsync().Result;
    
                    dynamic projects = JArray.Parse(responseString) as JArray;
    
                    foreach (var obj in projects)
                    {
                        DtoProject dto = obj.ToObject<DtoProject>();
    
                        dtoprojects.Add(dto);
                    }
                }
    
                return dtoprojects;
            }
    
            public DtoProject GetProjByIdApi(int id)
            {
                DtoProject dto;
    
                using (var client = new HttpClient())
                {
                    var uri = new Uri("http://progmgmntcore2api.com/api/project/GetProjById?id=" + id);
                    HttpResponseMessage getResponseMessage = client.GetAsync(uri).Result;
    
                    if (!getResponseMessage.IsSuccessStatusCode)
                        throw new Exception(getResponseMessage.ToString());
    
                    var responsemessage = getResponseMessage.Content.ReadAsStringAsync().Result;
    
                    dynamic project = JsonConvert.DeserializeObject(responsemessage);
    
                    dto = project.ToObject<DtoProject>();
                }
    
                return dto;
            }
    
            public void CreateProjectApi(DtoProject dto)
            {
                using (var client = new HttpClient { BaseAddress = new Uri("http://progmgmntcore2api.com") })
                {
                    string serailizeddto = JsonConvert.SerializeObject(dto);
    
                    var inputMessage = new HttpRequestMessage
                    {
                        Content = new StringContent(serailizeddto, Encoding.UTF8, "application/json")
                    };
    
                    inputMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
                    HttpResponseMessage message =
                        client.PostAsync("api/project/CreateProject", inputMessage.Content).Result;
    
                    if (!message.IsSuccessStatusCode)
                        throw new Exception(message.ToString());
                }
            }
    
            public void UpdateProjectApi(DtoProject dto)
            {
                using (var client = new HttpClient { BaseAddress = new Uri("http://progmgmntcore2api.com") })
                {
                    string serailizeddto = JsonConvert.SerializeObject(dto);
    
                    var inputMessage = new HttpRequestMessage
                    {
                        Content = new StringContent(serailizeddto, Encoding.UTF8, "application/json")
                    };
    
                    inputMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
                    HttpResponseMessage message =
                        client.PostAsync("api/project/UpdateProject", inputMessage.Content).Result;
    
                    if (!message.IsSuccessStatusCode)
                        throw new Exception(message.ToString());
                }
            }
    
            public void DeleteProjectApi(DtoId dto)
            {
                using (var client = new HttpClient { BaseAddress = new Uri("http://progmgmntcore2api.com") })
                {
                    string serailizeddto = JsonConvert.SerializeObject(dto);
    
                    var inputMessage = new HttpRequestMessage
                    {
                        Content = new StringContent(serailizeddto, Encoding.UTF8, "application/json")
                    };
    
                    inputMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
                    HttpResponseMessage message =
                        client.PostAsync("api/project/DeleteProject", inputMessage.Content).Result;
    
                    if (!message.IsSuccessStatusCode)
                        throw new Exception(message.ToString());
                }
            }
    

    Wednesday, May 22, 2019 12:56 PM
  • JSON with HTTP is generally referred to as a REST API. There are plenty of libraries available. In .NET itself HttpClient is the preferred option but WebClient can also be used. If you want third party support then RestClient or AutoRest can be used.

    None of these actually convert JSON though as REST is technically format agnostic. Most people use JSON.NET for JSON support but if you want to keep it framework only then DataContractSerializer(?) but this has some limitations. .NET Core 3 will ship with a full, built in JSON parser if you can wait that long. There are also other JSON parsers available that may work better for your needs.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, May 22, 2019 2:14 PM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 29, 2019 7:27 AM
    Moderator