locked
How to Create a Multi-Dimensional JSON Array Object RRS feed

  • Question

  • User-1203918395 posted

    Howdy!

      I need to create a JSON object that's going to look like this:

    {"0":{"name":"lastName","title":"Last Name"},"1":{"name":firstName","title":"First Name"},"2":{"name":"grade","title":"Grade"}}

      What I currently have is a JSON object that looks like this:

    [{"__type":"DMC.WebServices.eocColumn","name":"lastName","title":"Last Name"},{"__type":"DMC.WebServices.eocColumn","name":firstName","title":"First Name"},{"__type":"DMC.WebServices.eocColumn","name":"grade","title":"Grade"}]

    So obviously I have a couple of issues...the first of which is creating the multi-dimensional array.  My WebMethod looks like this:

    using DMC.Classes;
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.Services;
    
    namespace DMC.WebServices
    {
        public class eocColumn
        {
            public string name { get; set; }
            public string title { get; set; }
            public string type { get; set; }
        }
    
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        [System.Web.Script.Services.ScriptService]
        public class eoc : System.Web.Services.WebService
        {
            [WebMethod]
            public List<eocColumn> getEOCColumns()
            {
                formValues fv = new formValues();
                string pPersonID = fv.personID;
                List<eocColumn> eocColumns = new List<eocColumn>();
    
                string mySQL = "exec get_EOCTableHeaders2 @pPersonID";
                string cf = System.Configuration.ConfigurationManager.ConnectionStrings["DistrictAssessmentDWConnectionString"].ConnectionString;
    
                SqlConnection connection = new SqlConnection(cf);
                SqlCommand command = new SqlCommand(mySQL, connection);
    
                command.Parameters.AddWithValue("@pPersonID", pPersonID);
    
                connection.Open();
    
                SqlDataReader dr = command.ExecuteReader();
    
                while (dr.Read())
                {
                    eocColumn ec = new eocColumn();
                    ec.name = dr.GetValue(0).ToString();
                    ec.title = dr.GetValue(1).ToString();
                    ec.type = dr.GetValue(4).ToString();
    
                    eocColumns.Add(ec);
                }
    
                connection.Close();
    
                return eocColumns;
            }
        }
    }
    

    I'm guessing my outter array would just be an iteration for the number of rows in the inner array...but how do you create an array of two different types?

    Tuesday, January 27, 2015 1:54 PM

All replies

  • User616225559 posted

    The format its using is correct for a a JSON array, it should have [] brackets. 

    It is doing this because you have a list of objects, if you want a single object you can't use List<> as that is translated into an array for JSON.

    Tuesday, January 27, 2015 2:35 PM
  • User-1203918395 posted

    Is that what this is considered, a single object?  So technically what it be called "a multidimensional array inside a single object"?  I tend to overthink things after a while, and right now it's making my head spin because I've been trying to figure things out for 2 days now...

    {"0":{"name":"lastName","title":"Last Name"},"1":{"name":firstName","title":"First Name"},"2":{"name":"grade","title":"Grade"}}

    Also, I tried changing my method to look like below, but it's still producing the brackets:

            [WebMethod]
            public eocColumn[] getEOCColumns()
            {
                formValues fv = new formValues();
                string pPersonID = fv.personID;
                //List<eocColumn> eocColumns = new List<eocColumn>();
    
                string mySQL = "exec get_EOCTableHeaders2 @pPersonID";
                string cf = System.Configuration.ConfigurationManager.ConnectionStrings["DistrictAssessmentDWConnectionString"].ConnectionString;
    
                SqlConnection connection = new SqlConnection(cf);
                SqlCommand command = new SqlCommand(mySQL, connection);
    
                command.Parameters.AddWithValue("@pPersonID", pPersonID);
    
                connection.Open();
    
                SqlDataReader dr = command.ExecuteReader();
    
                eocColumn[] eocColumns = new eocColumn[12];
    
                int ct = 0;
                while (dr.Read())
                {
                    eocColumns[ct] = new eocColumn();
                    eocColumns[ct].name = dr.GetValue(0).ToString();
                    eocColumns[ct].title = dr.GetValue(1).ToString();
                    eocColumns[ct].type = dr.GetValue(4).ToString();
    
                    ct++;
                }
    
                connection.Close();
    
                return eocColumns;
            }

     

    Tuesday, January 27, 2015 3:27 PM
  • User2008642861 posted

    Hi puffman,

    From your post, I suppose that you want trigger the function of behind code at client side by JSON. Please refer to the similar case as below.

    http://stackoverflow.com/questions/1291076/how-do-i-convert-a-c-sharp-two-dimensional-array-to-a-json-object

    Hope this could be helpful to you.

    Best regards,

    Archer

    Wednesday, January 28, 2015 4:19 AM
  • User-1203918395 posted

    Archer Wang!  Thanks for taking the time to help.  I tried what you said, and got the results exactly as that link said I would.  Unfortunately, that is now the solution I need.

    I apologize to everybody who's trying to help, I'm not trying to be cryptic on purpose or anything, I guess I'm just not doing a good job of explaining what I need.  Let me try it again.  I'm wanting to create a dynamic Foo table.  The creator of foo gives an example where he simply hard codes the columns for the purpose of the example, like this:

        FAjax.defaults = {
            //columns: {},
            //columns: FAjax.eocColumns().done(function (columnResults) { FAjax.defaults.columns = JSON.parse(JSON.stringify(columnResults.d)) }),
            columns: {
                0: { name: 'lastName', title: 'Last Name' }, 
                1: { name: 'firstName', title: 'First Name' },
                2: { name: 'grade', title: 'Grade', type: 'number' }
            },
            dataSource: FAjax.eocResults,
            rows: {},
            sorters: {
                text: function (a, b) {
                    if (typeof (a) === 'string') { a = a.toLowerCase(); }
                    if (typeof (b) === 'string') { b = b.toLowerCase(); }
                    if (a === b) return 0;
                    if (a < b) return -1;
                    return 1;
                },
                number: function (a, b) {
                    return a - b;
                }
            }
        };

    When I do a JSON.stringify against those hard-coded column values, the result comes out looking like this:

    {"0":{"name":"lastName","title":"Last Name"},"1":{"name":"firstName","title":"First Name"},"2":{"name":"grade","title":"Grade"}}

    What I'm hoping to do, is to recreate these columns in a dynamic fashion by reading in values from my database.  I just assumed I'd need to create a multidimensional array, but perhaps that's not the answer?  Is this what a dictionary entry looks like?

    Wednesday, January 28, 2015 8:58 AM