使用json数据,jstree中的状态无法正常工作 [英] State is not working properly in jstree using json data
问题描述
大家好,
i需要在 asp.net c#代码后面生成json数据格式(服务器端)。
i具有下面给出的预期json数据格式,
[{ id: ajson2, parent: # , text: 根节点2, state:{ 选中: true }}]
如何在 asp.net c#代码后面生成上述格式。
asper我的知识我已生成以下格式但 此 case 我无法制作类似 state的格式: { selected: true }。
除了id和父 工作正常但状态 无法正常工作。
我的代码 :
< pre lang =C#> public string DataTableToJsonWithStringBuilder(DataTable table)
{
var jsonString = new StringBuilder();
if (table.Rows.Count > 0 )
{
jsonString.Append( [);
for ( int i = 0 ; i < table.Rows.Count; i ++)
{
jsonString.Append( {);
for ( int j = 0 ; j < table.Columns.Count; j ++)
{
if (j < table.Columns.Count - 1 )
{
jsonString.Append( \ + table.Columns [j] .ColumnName.ToString( )
+ \: + \
+ table.Rows [i] [j] .ToString()+ \,);
}
else if (j == table.Columns.Count - < span class =code-digit> 1 )
{
jsonString.Append( \ + table.Columns [j] .ColumnName.ToString()
+ \: + \
+ table.Rows [i] [j] .ToString()+ \) ;
}
}
if (i == table.Rows.Count - 1 )
{
jsonString.Append( }) ;
}
else
{
jsonString.Append( },);
}
}
jsonString.Append( ]) ;
}
return jsonString.ToString();
}
提前感谢..
我尝试了什么:
状态在使用json数据的jstree中无法正常工作
尝试使用以下代码:
public string DataTableToJsonWithStringBuilder(DataTable table)
{
var jsonString = new StringBuilder();
if (table.Rows.Count > 0 )
{
jsonString.Append( [);
for ( int i = 0 ; i < table.Rows.Count; i ++)
{
jsonString.Append( {);
for ( int j = 0 ; j < table.Columns.Count; j ++)
{
// 州列专用
if (table.Columns [j] .ColumnName = state)
{
CustomData data = Row [ column1] as CustomData;
if (data!= null ){
// 实现你的逻辑以生成精确的JSON字符串
}
}
}
}
}
}
尽管上面有一个解决方案,但维护这种类型的复杂场景有点困难。为了解决您的问题,请使用Json.Net DLL或JavaScriptSerializer类。请按照以下链接对您提供帮助:
3种方式在Asp.net C#中将数据转换为JSON - Codepedia.info [ ^ ]
Hi all,
i need to generate json data formate in asp.net c# code behind(server side).
i have an expected json data format given below,
[{ "id" : "ajson2", "parent" : "#", "text" : "Root node 2","state":{"selected":true} }]
how can i generate the above formate in asp.net c# code behind.
asper my knowlege i have generated below formate but in this case i cannot make a format like "state":{"selected":true}.
other than id and parent is working fine but state is not working properly.
my code is :
public string DataTableToJsonWithStringBuilder(DataTable table)
{
var jsonString = new StringBuilder();
if (table.Rows.Count > 0)
{
jsonString.Append("[");
for (int i = 0; i < table.Rows.Count; i++)
{
jsonString.Append("{");
for (int j = 0; j < table.Columns.Count; j++)
{
if (j < table.Columns.Count - 1)
{
jsonString.Append("\"" + table.Columns[j].ColumnName.ToString()
+ "\":" + "\""
+ table.Rows[i][j].ToString() + "\",");
}
else if (j == table.Columns.Count - 1)
{
jsonString.Append("\"" + table.Columns[j].ColumnName.ToString()
+ "\":" + "\""
+ table.Rows[i][j].ToString() + "\"");
}
}
if (i == table.Rows.Count - 1)
{
jsonString.Append("}");
}
else
{
jsonString.Append("},");
}
}
jsonString.Append("]");
}
return jsonString.ToString();
}
thanks in advance..
What I have tried:
state is not working properly in jstree using json data
Try with below code:
public string DataTableToJsonWithStringBuilder(DataTable table) { var jsonString = new StringBuilder(); if (table.Rows.Count > 0) { jsonString.Append("["); for (int i = 0; i < table.Rows.Count; i++) { jsonString.Append("{"); for (int j = 0; j < table.Columns.Count; j++) { // Special for state column if(table.Columns[j].ColumnName = "state") { CustomData data = Row["column1"] as CustomData; if (data != null) { // Implement your logic to generate exact JSON string } } } } } }
Although above one resolve but it is bit difficult to maintain this type of complex scenario. In order to resolve your problem either use Json.Net DLL or JavaScriptSerializer class. Follow below link that may help you:
3 Ways Convert Datatable to JSON in Asp.net C# - Codepedia.info[^]
这篇关于使用json数据,jstree中的状态无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!