简体   繁体   中英

How to convert a DataTable into JSON in spotfire using IronPython?

I have a datatable present in Spotfire, I need to convert it into JSON object. I have below working piece of code but I need code that works faster.

import clr
import sys
clr.AddReference('System.Web.Extensions')
from System.Web.Script.Serialization import JavaScriptSerializer
from Spotfire.Dxp.Data import IndexSet
from Spotfire.Dxp.Data import DataValueCursor

rowCount = MyTable.RowCount
rows = IndexSet(rowCount,True)
cols = MyTable.Columns
MyTableData=[]

for r in rows:
 list={}
 item={}
 for c in cols:
  item[c.Name] = c.RowValues.GetFormattedValue(r)
  list['MyData']=item
 MyTableData.append(list)

json=JavaScriptSerializer(MaxJsonLength=sys.maxint).Serialize(MyTableData)

I am not sure how much overhead the last line has on your code, but if you are using myTableData result as a string to drive a javscript widget, you dont need it. Try replacing last line with

json=str(MyTableData)

and see how fast it is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM