将控制台输出的代码更改为 JSON 输出

Change code for Console output to JSON output

我有一个代码可以读取 QAAWS 并将其打印到控制台,我现在想做的是创建一个 javascript,它将 运行 相同的代码但会保存它作为一个可以被网站使用的 JSON。我试着做 Debug.WriteLine() 而不是 Console.WriteLine() 但它没有用。

我之前写过代码来读取 XML 并将其转换为 JSON 但不知何故这给了我更多问题。这是在控制台中读取它的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApp1.TestWeb;

namespace ConsoleApp1 {
    class Program {
        static void Main(string[] args) {
            ConsoleApp1.TestWeb.QAAWS_by_USER test1 = new
            ConsoleApp1.TestWeb.QAAWS_by_USER();
            String message, creatorname, creationdateformatted, description, universe;
            DateTime creationdate;
            int queryruntime, fetchedrows;
            ConsoleApp1.TestWeb.Row[] row = test1.runQueryAsAService("<username>", "<password>", out message, out creatorname, out creationdate, out creationdateformatted, out description, out universe, out queryruntime, out fetchedrows);
            int resultCount = row.Length;
            for (int i = 0; i < resultCount; i++) {
                Console.WriteLine(row[i].User + " " + row[i].Owed);
            }
            Console.Read();
        }
    }
}

如果您需要任何其他信息,请告诉我。

我们在工作中使用了 QAAWS,因此您可以使用以下方法。与其使用控制台应用程序,不如创建一个 asmx Web 服务。该服务将包含一个 class,它将 return 一个 ArrayList,而 asmx 文件将获取列表和 return JSON。这是它的样子

TestWebService.asmx.cs

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

namespace TestWebService {
    /// <summary>
    /// Summary description for TestService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class TestService : System.Web.Services.WebService {

        private LoadService user;

        public TestService() {
            this.user = new LoadService();
        }

        [WebMethod]
        public string TestResult() {
            ArrayList list = this.user.getTestUser();

            return JsonConvert.SerializeObject(list);
        }
    }
}

这里是 TestWebService.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using Newtonsoft.Json;

namespace TestWebService {
    public class LoadService {
        public ArrayList getTestUser() {
            TestWebService.TestWeb.QAAWS_by_USER test1 = new
            TestWebService.TestWeb.QAAWS_by_USER();
            String message, creatorname, creationdateformatted, description, universe;
            DateTime creationdate;
            int queryruntime, fetchedrows;
            TestWebService.TestWeb.Row[] row = test1.runQueryAsAService(("<username>", "<password>", out message, out creatorname, out creationdate, out creationdateformatted, out description, out universe, out queryruntime, out fetchedrows);
            int resultCount = row.Length;
            var index = 0;
            var list = new ArrayList();
            for (int i = 0; i < resultCount; i++) {
                getUserInformation userInformation = new getUserInformation {
                    User_name = row[i].User,
                    Owed_value = row[i].Owed
                };
                list.Add(userInformation);
                index++;
            }
            return list;
        }
    }
    public class getUserInformation {
        public string User_name { get; set; }
        public double Owed_value { get; set; }
    }
}