Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have the following json:

HTML
{
"Header": {
"MCC": "415",
"FO": "0",
"REGID": "5"
 },
"Contacts": [
{
    "NAME": "jocelyne",
    "MO": "70123456"
},
{
    "NAME": "eliane",
    "MO": "03123456"
}
] }

I have 2 clients: one client is giving me this json in this order, the second is giving me the mo before name in the json, but I need to have the same order in the datatable all the time when I deserialize the json.

What I need to know which is the better way?

1-to deserialize the json in a list (it takes the tags in the alphabetic order) and then convert the list to a datatable

in this case that's what i'm doing:

VB
Public Class Dataa
Public MCC As Integer
Public FO As Integer
Public RegId As Integer
Public Contacts As Contacts()
End Class

<Serializable()> _
Public Class Contacts
Public name As String
Public mo As String
End Class

 Dim jss As New JavaScriptSerializer
    Dim oFareResults As Generic.List(Of Dataa) = jss.Deserialize(Of List(Of Dataa))(json)
    contactsDT = ConvertListToDataTable(oFareResults)

    contactsDT.Columns.Add("taken_mo", GetType(String))
    contactsDT.Columns.Add("reg_id", GetType(Integer))

        Private Shared Function ConvertListToDataTable(oFareResults As List(Of Dataa)) As DataTable
    ' New table.
    Dim dtTable As New DataTable()


    dtTable.Columns.Add("name", GetType(String))
    dtTable.Columns.Add("mo", GetType(String))

    For i As Integer = 0 To oFareResults.Item(0).Contacts.Length - 1
        dtTable.Rows.Add(oFareResults(0).Contacts(i).name, oFareResults(0).Contacts(i).mo)
    Next
    Return dtTable
End Function

2-to select 2 cases, every case for a different client and then deserialize the json in a datatable

in this case:

VB
 Public Class Data
Public Header As Header
Public Contacts As DataTable
End Class

<System.Runtime.Serialization.DataContract()>
Public Class Header
<System.Runtime.Serialization.DataMember(Name:="MCC")>
Public MCC As Integer
<System.Runtime.Serialization.DataMember(Name:="FO")>
Public FO As Integer
<System.Runtime.Serialization.DataMember(Name:="REGID")>
Public RegId As Integer
End Class

    Dim data As New Data
    data = JsonConvert.DeserializeObject(Of Data)(json)
    mcc = data.Header.MCC
    FO = data.Header.FO
    regid = data.Header.RegId
    contactsDT = data.Contacts

that's how i have to deserialize the json into a datatable and in this case i have to have 2 functions to deserialize one for every client ( to reserve the order that i need)

Well, what I need to know is not the general solution but I need to know which one has a better performance for the server considering that my json may be very long.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900