简体   繁体   English

我如何在网络服务 c# 中使用 json 文件

[英]How i can use a json file in a webservice c#

Hi i made a webservice with visual studio 2017.嗨,我用 Visual Studio 2017 做了一个网络服务。

But now i want to get a json file and show it inside my webservice但现在我想获得一个 json 文件并将其显示在我的网络服务中

instead of "idVersion" wrote in the code, i want to read a file like "idVersions.json"而不是在代码中写的“idVersion”,我想读取一个像“idVersions.json”这样的文件

an example of the solution organization解决方案组织示例

Sln 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);
        }

    }
}

You can read the JSON directly from your sourcecode like below.您可以直接从源代码中读取 JSON,如下所示。

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.一旦你得到 json 字符串,你可以序列化到你的 object。

Use File.ReadAllText and return this String?使用 File.ReadAllText 并返回此字符串?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM