1

カスタムデータセットをシリアル化しようとすると、このエラーが発生します。私は何を間違っているのですか、確かにそれは単純なはずですか?

ありがとう

エラータイプ'System.Globalization.CultureInfo'のオブジェクトのシリアル化中に循環参照が検出されました。

 Dim serializer As New JavaScriptSerializer()
            Dim arrayJson As String = serializer.Serialize(makeMYDataSET())

    Private Function makeMYDataSET() As DataSet


        ' Two DataTables.
        Dim table1 As DataTable = New DataTable("patients")
        table1.Columns.Add("name")
        'table1.Columns.Add("id")
        table1.Rows.Add("sam")
        table1.Rows.Add("mark")
        table1.Rows.Add("hjhkhkh")

        Dim table2 As DataTable = New DataTable("medications")
        'table2.Columns.Add("id")
        table2.Columns.Add("medication")
        table2.Rows.Add("atenolol")
        table2.Rows.Add("amoxicillin")

        ' Create a DataSet. Put both tables in it.
        Dim set1 As DataSet = New DataSet("office")
        set1.Tables.Add(table1)
        set1.Tables.Add(table2)


        Return set1

    End Function
4

2 に答える 2

1

JavaScriptSerializerプレーンで使用することはできませんDataSet


1つの方法は、DataSetXMLに変換するか、より適切には、Dictionary最初のXMLに変換することです。

このメソッドを使用できます(CodeProjectの例):

Function DataSetToJSON(ds As DataSet) As String
    Dim dict = New Dictionary(Of String, Object)

    For Each dt As DataTable In ds.Tables
        Dim arr(dt.Rows.Count) As Object

        For i = 0 To dt.Rows.Count - 1
            arr(i) = dt.Rows(i).ItemArray
        Next

        dict.Add(dt.TableName, arr)
    Next

    Dim json = New JavaScriptSerializer
    Return json.Serialize(dict)
End Function

 DataSetToJSON(makeMYDataSET())

結果

{"patients":[["sam"]、["mark"]、["hjhkhkh"]、null]、 "medications":[["atenolol"]、["amoxicillin"]、null]}

于 2012-08-27T06:28:33.030 に答える
-1
DataSet somedataset;
XmlSerializer s = new XmlSerializer(typeof(Dataset));
StringWriter sw = new StringWriter();
s.Serialize(sw, somedataset);
string serialized = sw.ToString();

そのc#は知っていますが、機能します。
javascriptserialzerクラスについては、これまで使用したことがありませんが、xmlserializerに関連していると思います。

于 2012-08-27T06:13:04.973 に答える