将Console输出的代码更改为JSON输出

时间:2015-06-29 16:59:32

标签: javascript c# json debugging console.writeline

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

如果您还需要其他任何信息,请与我们联系。

1 个答案:

答案 0 :(得分: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; }
    }
}