Search code examples
javascriptc#jsondebuggingconsole.writeline

Change code for Console output to JSON output


I have a code to read a QAAWS and prints it out to the console, what I want to do now is to instead create a javascript that will run the same code but will save it as a JSON that can be used by a website. I tried to do Debug.WriteLine() instead of Console.WriteLine() which it has not worked.

I have writen code before to read an XML and convert it to a JSON but somehow this is giving me more issue. Here is the code to read it in the console:

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

Let me know if there is any other information you need.


Solution

  • We used QAAWS at work, so here is an approach you can use. Instead of using a console application better create an asmx Web service. The service will contain a class that will return an ArrayList and the asmx file will take the list and return the JSON. Here is how it would look

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

    And here is the 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; }
        }
    }