且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何创建优化json字符串

更新时间:2022-10-15 09:19:03

这个问题不合适;这个问题根本没有意义。没有优化或未优化的JSON。



您只是在谈论完全不同的JavaScript对象。当然,他们的序列化形式是不同的。

比较:

var a = [ 1 some text null ];
var b = {number: 1 ,text: 某些文字,参考: null };

var sA = JSON .stringify(a);
// sA变为'[1,some text,null]'
var sB = JSON .stringify(b);
// sB变为{数字:1,text:some text,引用:null}





这些对象都是对象类型,但它们是不同的。第一个对象具有名为0,1和2的属性。指数实际上与属性没有区别,它们只使用专门的名称。第二个对象具有名为number,text和reference的类似属性。另一个区别是:第一个对象,作为数组,具有属性长度,在第二个对象中没有定义。



请参阅:

JavaScript数据类型和数据结构 - JavaScript

https://developer.mozilla.org/en-US/docs/ Web / JavaScript / Reference / Operators / typeof

对象 - JavaScript

数组 - JavaScript

JSON - JavaScript



现在关于压缩......你的内容根本就不是压缩;这只是拥有不同的对象。顺便说一下,在大多数情况下,更大的物体更为可取。您可以压缩和解压缩任何字符串,无论它是否为JSON。有很多图书馆在做这件事。仅举几例:

GitHub - pieroxy / lz-string:基于LZ的JavaScript压缩算法

LZW javascript压缩/解压缩 - GitHub

另请参阅面向Web开发人员的文本压缩 - HTML5 Rocks



-SA


那不是json。如果你想使用优化的格式来节省空间,那么只需编写自己的序列化和反序列化代码而不是使用json库,因为这些库不会处理你想要的文本,因为它不是json。然后,您可以将数据作为普通字符串传递,并在目标方法中对其进行反序列化。


试试这个



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

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

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

}


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

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:
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


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.


try this

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

   }