【问题标题】:Change code for Console output to JSON output将控制台输出的代码更改为 JSON 输出
【发布时间】:2015-06-29 16:59:31
【问题描述】:

我有一个代码可以读取 QAAWS 并将其打印到控制台,我现在要做的是创建一个 javascript 来运行相同的代码,但会将其保存为 JSON 以供 a网站。我尝试做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();
        }
    }
}

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

【问题讨论】:

    标签: javascript c# json debugging console.writeline


    【解决方案1】:

    我们在工作中使用了 QAAWS,因此您可以使用以下方法。与其使用控制台应用程序,不如创建一个 asmx Web 服务。该服务将包含一个返回 ArrayList 的类,asmx 文件将获取该列表并返回 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; }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-26
      相关资源
      最近更新 更多