1

I need to create JSON file in the following format:

"Result": [
         [ "Date", "Name", "Address", "Age" ],
         [ "MMDDYYYY", "Name1", "Add1", "15" ],
         [ "MMDDYYYY", "Name2", "Add2", "20" ]

The Result's data is a List<JArray> which is filled by List<string> which is filled from a DataTable as following:

var jArrayList = new List<JArray>();
var rowData = new List<string>();
for (int i = 0; i < table.Rows.Count; i++)
{
   var rowData = new List<string>(table.Columns.Count);
   DataRow dataRow = table.Rows[i];

   foreach (DataColumn dataColumn in table.Columns)
   {
      if (i > 0)
      {
         rowData.Add(dataRow[dataColumn].ToString());
      }
    }

    if (rowData.Any())
    {
       jArrayList.Add(new JArray(rowData));
    }
 }

 if (jArrayList.Any())
 {
    var jobj = new JObject( new JProperty("Result",
                            from p in jArrayList
                            select p));
     jObjList.Add(jobj);
  }

  var outputJson = new JObject(new JProperty("Entries",
                                        from p in jObjList
                                        select p));
  var outputString = outputJson.ToString();

When i execute this code it is writing everything in new line:

"Result": [
    [
      "Date",
      "Name",
      "Address",
      "Age"
    ],
    [
      "MMDDYYYY",
      "Name1",
      "Add1",
      "15"
    ],
    [
      "MMDDYYYY",
      "Name2",
      "Add2",
      "20"
    ],

How to format it to be written in this way?

"Result": [
     [ "Date", "Name", "Address", "Age" ],
     [ "MMDDYYYY", "Name1", "Add1", "15" ],
     [ "MMDDYYYY", "Name2", "Add2", "20" ]
6
  • Sorry for probably ignorant question, but why you need to create it in this exact format ? Commented Nov 4, 2016 at 9:10
  • @mybirthname because it will have a very large data. So with new lines it will be more size and not readable. Commented Nov 4, 2016 at 9:13
  • You should not be displaying a low level format to users anyway - JSON is not meant to be seen by users - that's what UIs are for! Commented Nov 4, 2016 at 9:15
  • @toadflakz it will not be used for end-user, it is for another level of development which sometimes needs observation. I am just asking if it is possible or it is impossible? Commented Nov 4, 2016 at 9:18
  • it is possible, but you will need to parse it through an algorithm or a regex Commented Nov 4, 2016 at 9:26

1 Answer 1

2

Here:

string json = "\"Result\": [\r\n    [\r\n      \"Date\",\r\n      \"Name\",\r\n      \"Address\",\r\n      \"Age\"\r\n    ],\r\n    [\r\n      \"MMDDYYYY\",\r\n      \"Name1\",\r\n      \"Add1\",\r\n      \"15\"\r\n    ],\r\n    [\r\n      \"MMDDYYYY\",\r\n      \"Name2\",\r\n      \"Add2\",\r\n      \"20\"\r\n    ],";
            var withoutEnter = json.Replace(Environment.NewLine, "");
            var splited = withoutEnter.Split('[');
            var formattedJson = splited[0];
            for (int i = 1; i < splited.Length; i++)
            {
                formattedJson = formattedJson + "[" + splited[i] + "\n";
            }

I have copy-pasted your json sample and parsed it to remove all enters, then added back enter for each group. Sorry for the messy code :)

Sign up to request clarification or add additional context in comments.

2 Comments

Your solution is removing [ which must be there
well, you definitely gave me the idea .. But instead of use Split() I will use Replace() As well Json.ToString(Formatting.None) and no need for loop Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.