Lara Lara - 4 months ago 16
JSON Question

Formating Json data from string and Datatable

I have a situation where I need to format json data into below .

[WebMethod]
public static string ConvertDatadttoString(string appName)
{
var sample = new List<SampleClass>
{
new SampleClass()
{
columns = new List<SampleItem>()
{
new SampleItem() {title = "NAME" },
new SampleItem() {title = "COUNTY" },
},
data = new List<List<string>>()
{
new List<string> { "John Doe", "Fresno" },
new List<string> { "Billy", "Fresno" },
new List<string> { "Tom", "Kern" },
new List<string> { "King Smith", "Kings" },
}
}
};

var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(sample);

return json;
}

public class SampleClass
{
public IEnumerable<SampleItem> columns { get; set; }
public IEnumerable<IEnumerable<string>> data { get; set; }
}

public class SampleItem
{
public string title { get; set; }
}


The above code is giving the correct json data for me, but I need to do some modifications in the code so that it can intake data from my source.First of all
new SampleItem() {title = "NAME" },
needs to be populated from stringarray which is like

string columnNames = "Name,County";
string[] arrcolumnNames = columnNames.Split(',');


and secondly,
new List<string> { "John Doe", "Fresno" },
needs to be populated from .net
DataTable
. How to do it.

I tried to add for loop and add the values from string array:

for(int i = 0; i <= arrcolumnNames.Length; i++)
{
new SampleItem() { title = arrcolumnNames[i] }
}


but I'm getting an error.

Update

[WebMethod]
public static string ConvertDatadttoString(string appName)
{

string columnNames = "Name,County";
string[] arrcolumnNames = columnNames.Split(',');

List<SampleItem> sampleItems = new List<SampleItem>();
for (int i = 0; i < arrcolumnNames.Length; i++)
{
sampleItems.Add(new SampleItem() { title = arrcolumnNames[i] });
}

var sample = new List<SampleClass>()
{
new SampleClass()
{
columns = sampleItems,

}

},

data = new List<List<string>>()
{
new List<string> { "John Doe", "Fresno" },
new List<string> { "Billy", "Fresno" },
new List<string> { "Tom", "Kern" },
new List<string> { "King Smith", "Kings" },
}
}
};

var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(sample);
return json;
}
}

Answer

Try this

public static string ConvertDatadttoString(string appName = "")
{

    var columnNames = "Name,County";
    var employees = CreateEmployeesTable();
    var sample = new List<SampleClass>
    {
        new SampleClass()
        {
            columns = columnNames.Split(',').Select(x=> new SampleItem
            {
                title = x
            }),
            data = employees.AsEnumerable().Select(row=> new List<string>
            {
                row.Field<string>("EmployeeName"),
                row.Field<string>("Company")
            })
        }
    };

    var serializer = new JavaScriptSerializer();
    var json = serializer.Serialize(sample);

    return json;
}

The private method CreateEmployeesTable

private DataTable CreateEmployeesTable()
{
    var table = new DataTable()
    {
        Columns = { "EmployeeName", "Company" },
        TableName = "Employees"
    };
    table.Rows.Add("John Doe", "Fresno");
    table.Rows.Add("Billy", "Fresno");
    table.Rows.Add("Tom", "Kern");
    table.Rows.Add("King Smith", "Kings");
    return table;
}
Comments