1

I've created a datatable using json string, though, some headers in my json string contains an array like this:

[{"Header1":"123","Header2":["asdasda"]}]

I successfully created a datatable using json but it doesn't show up the header that contains an array value like in the Header2. Here is my sample code in creating my datatable:

 strJson = Session("JsonStr").ToString
Dim tb As DataTable
tb = JsonConvert.DeserializeObject(Of DataTable)(strJson.ToString)

and everytime I try to fetch datatable to string, it appears. However, it only shows System.string[]. Using the json example I gave, the output is:

Header1=123 | Header2=System.string[]

Instead of fetching System.string[], can it fetch the value which is in array ("asdasda")? Here's my code in transfering datatable to string:

            Dim dr As DataRow
            Dim dc As DataColumn
            Dim sConcat As String = ""
            For Each dr In UpdatedTable.Rows
                For Each dc In UpdatedTable.Columns
                    sConcat = sConcat + dc.ColumnName + "= " + dr.Item(dc) + " | "
                Next
            Next dr
user692942
  • 16,398
  • 7
  • 76
  • 175
Kuroi
  • 212
  • 2
  • 15

1 Answers1

1

You are on the right track, but it would probably be easier to deserialize to a Type. Otherwise if the second column is an array then you need to access the data in it by index. You are seeing System.string[] (string array) in the results because you are simply referencing the array object not the data in the array:

dr.Item(dc)(0)

Since you cant know ahead of time how many items will be in the array for your loop, you need to convert column 1's data back into an array - the Row.Item are all Object so they can hold any sort of data.

' sample json, with 2 Hdr2 items
' the XML is just an easy way to have strings with quotes
' I added an item for testing purposes
Dim jstr = <str>
               [{"Header1":"123","Header2":["asdasda", "ziggy"]}]
            </str>.Value

Dim dt = JsonConvert.DeserializeObject(Of DataTable)(jstr)

Dim sRowData As String = ""
For n As Integer = 0 To dt.Rows.Count - 1
    sRowData = dt.Columns(0).ColumnName & " = " & dt.Rows(n).Item(0).ToString & " | "
    sRowData &= dt.Columns(1).ColumnName & " = "

    ' convert col 1 to an array, Join the elements
    Dim names As String() = CType(dt.Rows(n).Item(1), String())
    sRowData &= String.Join(", ", names)

    Console.WriteLine(sRowData)
Next n

Output:

Header1 = 123 | Header2 = asdasda, ziggy

I avoided having to iterate the array by using String.Join but iterating would be simple:

Dim names As String() = CType(dt.Rows(n).Item(1), String())

For j As Int32 = 0 To names.Length - 1
    sRowData &= names(j)
Next

Since dt.Rows(n).Item(1) is an object which we cannot iterate, you still need to convert it to a string array before looping on it.

Finally, you should turn on Option Strict. Since the cell/item data is stored as Object, with Option Strict on, the compiler would tell you this sort of code: + dr.Item(dc) + " | " isnt quite right.

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • What is `Dim jstr = [{"Header1":"123","Header2":["asdasda", "ziggy"]}] .Value`? – user692942 Aug 26 '15 at 15:39
  • it is an XML literal - an easy way to paste/type quotes into a string. It is sometimes used in [SQL Statements](http://stackoverflow.com/a/29187199/1070452) to allow formatting – Ňɏssa Pøngjǣrdenlarp Aug 26 '15 at 15:41
  • VB.NET (not VBScript) since VS2010 (maybe 2012) as I recall – Ňɏssa Pøngjǣrdenlarp Aug 26 '15 at 15:44
  • Realised this isn't Classic ASP fooled by the OP again! – user692942 Aug 26 '15 at 15:45
  • It doesnt have much to do with the question - I wanted to show the new test JSON and did that rather than paste a new json quote. The OP didnt say where they were getting it from (DL, their own serialier etc) – Ňɏssa Pøngjǣrdenlarp Aug 26 '15 at 15:49
  • i've tried your code and it gets the error message `Unable to cast object of type 'System.String' to type 'System.String[]'.` so i turned on the Option Strict in the properties but it disallows conversions object to datatable. i dont know if i didnt do it the right way or so, but i also tried the quick action so that the session can be read but it got the same error message – Kuroi Aug 27 '15 at 01:56
  • You should edit your question with the code you used and *where* you get the exception, The answer code works fine with the sample data you gave and with moire than item in the string array. As for Option Strict, thats what it does - tells you where your code is asking VB to *guess* about conversions - it typically offers a remedy in the drop down, – Ňɏssa Pøngjǣrdenlarp Aug 27 '15 at 12:02