0

Hi i made a webservice with visual studio 2017.

But now i want to get a json file and show it inside my webservice

instead of "idVersion" wrote in the code, i want to read a file like "idVersions.json"

an example of the solution organization

Sln

Someone can help me? thank you in advance :)

here's my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using Newtonsoft.Json;


namespace getLastVersionNumber
{
    /// <summary>
    /// Description résumée de WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // Pour autoriser l'appel de ce service Web depuis un script à l'aide d'ASP.NET AJAX, supprimez les marques de commentaire de la ligne suivante. 
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
        DataTable idVersions = new DataTable();
      
       

        

        [WebMethod]
        public string idVersion()
        {
            idVersions.Columns.Add("id");
            idVersions.Columns.Add("Version");

            idVersions.Rows.Add("1", "v0.1");
            idVersions.Rows.Add("2", "v0.2");
            idVersions.Rows.Add("3", "v0.2.5");
            idVersions.Rows.Add("4", "v1.2");
            idVersions.Rows.Add("5", "v2.5");
            idVersions.Rows.Add("6", "v3");

            return JsonConvert.SerializeObject(idVersions);
        }

    }
}

2 Answers2

0

You can read the JSON directly from your sourcecode like below.

string json = "";
using (WebClient wc = new WebClient())
{
   json = wc.DownloadString("https://jsonplaceholder.typicode.com/todos/1");
}

Once you get the json string, you can serialize to your object.

vaseef
  • 11
  • 2
  • hi @vaseef i tried you suggestion but webclient not recognized it's a nugget package i have to download? – Saif Ejjilali Jun 21 '20 at 11:27
  • WebClient class is part of the namespace System.Net. You can either modify the line as "using (System.Net.WebClient wc = new System.Net.WebClient())" or add a namespace "using System.Net;" – vaseef Jun 22 '20 at 16:17
0

Use File.ReadAllText and return this String?

Daniel
  • 103
  • 1
  • 2
  • 9