Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello,

I want to create json string like this form my C# code like this :

[
["Joe", 42,185,"M"],
["Ann",40, 125, "F"]
]

Currently i am creating like this

[
{"Name":"Joe", "Age":"42","Weight":185,"Gender":"M"},
{"Name":"Ann", "Age":"40","Weight":125,"Gender":"F"}
]

I am reading the C# Data Table and creating the JSON list.

I don't want to repeat the field names so that the JSON data is of less size for better bandwidth.

Can any one help me.

What I have tried:

public static string getJSONString(DataTable dt)
{
System.Web.Script.Serialization.JavaScriptSerializer JSSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
List<dictionary><string,>> dic = new List<dictionary><string,>>();
Dictionary<string,> newrow = null;
JSSerializer.MaxJsonLength = 2147483647;
//Code to loop each row in the datatable and add it to the dictionary object

dt.AcceptChanges();
string reqLink = "";

foreach (DataRow drow in dt.Rows)
{
newrow = new Dictionary<string,>();
foreach (DataColumn col in dt.Columns)
{
newrow.Add(col.ColumnName.Trim(), drow[col]);
}
dic.Add(newrow);
}
//Serialising the dictionary object to produce json output
return JSSerializer.Serialize(dic);
}
Posted
Updated 13-Jun-16 7:22am

The problem is ill-posed; and the question simply makes no sense. There is no "optimized" or not-optimized JSON.

You are just talking about completely different JavaScript objects. Naturally, their serialized forms are different.
Compare:
JavaScript
var a = [1, "some text", null];
var b = {number: 1, text: "some text", reference: null};

var sA = JSON.stringify(a);
// sA becomes '[1,"some text",null]'
var sB = JSON.stringify(b);
// sB becomes '{"number":1,"text":"some text","reference":null}'


Those objects are both of the object types, but they are different. First object has properties named 0, 1 and 2. Indices are actually no different from properties, they just use specialized names. The second object has analogous properties named "number", "text" and "reference". Another difference is: first object, as an array, has property "length", which is not defined in the second one.

Please see:
JavaScript data types and data structures — JavaScript,
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof,
Object — JavaScript,
Array — JavaScript,
JSON — JavaScript.

Now about compressing… what you though of is not compression at all; this is just having different objects. By the way, the "bigger" object is more preferable in most cases. You can compress and decompress any string, no matter if it is JSON or not. There is a number of libraries doing that. To name just a few:
GitHub — pieroxy/lz-string: LZ-based compression algorithm for JavaScript,
LZW javascript compress/decompress — GitHub,
see also Text Compression for Web Developers — HTML5 Rocks.

—SA
 
Share this answer
 
v3
Comments
Ajay_Saini 13-Jun-16 22:25pm    
Thanks SA.
You are right my purpose is only to compress the data no matter if it is string, array or JSON.

I have put one example of JSON in my question because i am using JSON object which have huge amount of data and it takes more than 30 mb sometimes...
So i was thinking about the way how to optimize that case, hence putting thought of optimization and compression.

I had seen the link for compression that you had shared and will try to implement the same. Lets see what impact it does in the above case.
Sergey Alexandrovich Kryukov 13-Jun-16 22:29pm    
All right. Is my answer clear? Are you going to accept it formally?
—SA
Ajay_Saini 24-May-17 0:18am    
I had used compression mechanism and it works.
That's not json. If you want to use an optimised format to save space then simply write your own serialisation and deserialization code rather than using json libraries as those libraries won't handle the text you want as it isn't json. You can then pass your data as a plain string and deserialise it in your target method.
 
Share this answer
 
v2
Comments
Ajay_Saini 13-Jun-16 12:05pm    
So can you share few example of that? Actually i am looking an approach so that i can avoid the field names in every result set to reduce the space.
F-ES Sitecore 13-Jun-16 12:20pm    
Something like this

string text = "[\"Joe\", 42,185,\"M\"]";
string[] values = text.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

string name = TrimText(values[0]);
int age = int.Parse(TrimText(values[1]));
int weight = int.Parse(TrimText(values[2]));
string gender = TrimText(values[3]);

---

public string TrimText(string text)
{
return text.TrimStart(new[] { '[', '\"' }).TrimEnd(new[] { ']', '\"' });
}
Sergey Alexandrovich Kryukov 13-Jun-16 13:26pm    
Sorry, this is absolutely irrelevant and redundant thing.
—SA
Sergey Alexandrovich Kryukov 13-Jun-16 13:27pm    
Sorry, what you say is simply not true. This is JSON, too, only of a very different object.
Please see Solution 3.
—SA
F-ES Sitecore 14-Jun-16 4:26am    
It isn't JSON, something having square brackets and commas doesn't make it JSON. Instead it is a way to define an array in javascript, however if you are sending that to .net code then .net can't interpret that as an array as easily as javascript can.
try this

C#
public static string getJSONString(DataTable dt)
   {
       System.Web.Script.Serialization.JavaScriptSerializer JSSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();

       List<List<object>> lst = new List<List<object>>();
       dt.AcceptChanges();

       foreach (DataRow drow in dt.Rows)
       {
           List<object> temp = new List<object>();
           foreach (DataColumn col in dt.Columns)
               temp.Add(drow[col]);
           lst.Add(temp);
       }
       return JSSerializer.Serialize(lst);

   }
 
Share this answer
 
Comments
Ajay_Saini 13-Jun-16 12:08pm    
In my code i have used dictionary and you are using list. So i am not sure if it will effect more in terms of space of retured JSON string.
Karthik_Mahalingam 13-Jun-16 12:10pm    
List is light weight than dictionary
Ajay_Saini 13-Jun-16 12:15pm    
Yes i new it, definitely it will reduce the time to convert the JSON but if the JSON sting takes approx 8 mb data then i can't utilize the bandwidth of network. And definitely if returned JSON string can be optimize or if any compression mechanism is available which client also understands then it will improve the performance.
Karthik_Mahalingam 13-Jun-16 12:23pm    
AFAIK, you cannot compress data
what you can do is, apply pagination in view/html where the data is displayed.
Sergey Alexandrovich Kryukov 13-Jun-16 13:25pm    
It's always possible to compress data. The problem is the inquirer's misconception about it.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900