none
Json Conversion Failed RRS feed

  • Question

  • I'm having issues with casting a josn string to a object:

    {"Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Collage.Domain.Models.Order.Order'."}

    'Convert Json to Model
                    _Order = CType(JsonConvert.DeserializeObject(_JSON), Domain.Models.Order.Order)


    Order Model:

    Namespace Domain.Models.Order
        Public Class Order
            Public Property api_key As String
            Public Property order_id As String
            Public Property shipping_first_name As String
            Public Property shipping_last_name As String
            Public Property shipping_address1 As String
            Public Property shipping_address2 As String
            Public Property shipping_city As String
            Public Property shipping_state As String
            Public Property shipping_zip As String
            Public Property shipping_country As String
            Public Property shipping_email As String
            Public Property shipping_phone As String
            Public Property shipping_method As String
            Public Property items As Item()
        End Class
    
        Public Class Image
            Public Property page As Integer
            Public Property url As String
        End Class
    
        Public Class Item
            Public Property sku As String
            Public Property quantity As Integer
            Public Property substrate As String
            Public Property diecut_shape As String
            Public Property images As Image()
        End Class
    End Namespace

    Json Code Being built to be passed in

    Dim Json As String
            Dim serializer As New System.Web.Script.Serialization.JavaScriptSerializer()
    
            oOrder.api_key = ""
            oOrder.order_id = "000001"
            oOrder.shipping_address1 = "123"
            oOrder.shipping_address2 = "456"
            oOrder.shipping_city = "Test"
            oOrder.shipping_country = "US"
            oOrder.shipping_email = "test@visionps.com"
            oOrder.shipping_first_name = "Test"
            oOrder.shipping_last_name = "User"
            oOrder.shipping_method = "USPS"
            oOrder.shipping_phone = "111-111-1111"
            oOrder.shipping_state = "IL"
            oOrder.shipping_zip = "60901"
    
            Json = serializer.Serialize(oOrder)


    Json String being passed in:

    "{""api_key"":"""",""order_id"":""000001"",""shipping_first_name"":""Test"",""shipping_last_name"":""User"",""shipping_address1"":""123"",""shipping_address2"":""456"",""shipping_city"":""Test"",""shipping_state"":""IL"",""shipping_zip"":""60901"",""shipping_country"":""US"",""shipping_email"":""test@visionps.com"",""shipping_phone"":""111-111-1111"",""shipping_method"":""USPS"",""items"":null}"

    Friday, October 11, 2019 3:15 PM

Answers

  • Check this:

      _Order = JsonConvert.DeserializeObject(Of Domain.Models.Order.Order)( _JSON )

    • Marked as answer by old_School Monday, October 14, 2019 1:07 PM
    Friday, October 11, 2019 4:10 PM

All replies

  • I made a few corrections to my code but still getting an error:

    Input

    Private Sub TestImport()
            Dim import As Importer, response As Domain.Structures.Structures.ReturnResponse
            Dim oOrder As New Domain.Models.Order.Order
            Dim Json As String
            Dim serializer As New JsonSerializer
    
            oOrder.api_key = ""
            oOrder.order_id = "000001"
            oOrder.shipping_address1 = "123"
            oOrder.shipping_address2 = "456"
            oOrder.shipping_city = "Test"
            oOrder.shipping_country = "US"
            oOrder.shipping_email = "test@visionps.com"
            oOrder.shipping_first_name = "Test"
            oOrder.shipping_last_name = "User"
            oOrder.shipping_method = "USPS"
            oOrder.shipping_phone = "111-111-1111"
            oOrder.shipping_state = "IL"
            oOrder.shipping_zip = "60901"
    
            Json = JsonConvert.SerializeObject(oOrder)
    
            import = New Importer(Json)
            response = import.ImportOrder
        End Sub

    Output

    Try
                    'Convert Json to Model
                    _Order = CType(JsonConvert.DeserializeObject(_JSON), Domain.Models.Order.Order)
                    DisplayMessage(String.Format("Processing order: {0}...", _Order.order_id))
    
                    _DB.LoadOrderAdapter(_Order_da, _Order_dt, _Order_cb)
                    _DB.LoadItemAdapter(_Item_da, _Item_dt, _Item_cb)
                    _DB.LoadProductionItemAdapter(_Production_da, _Production_dt, _Production_cb)
                    _DB.LoadPageAdapter(_Page_da, _Page_dt, _Page_cb)
    
                    oResponse = Process()
    
                Catch ex As Exception
                    Utils.ErrorEmail("Import ERROR!", ex, AttachmentName:="Import.xml", AttachmentData:=_XML)
    
                    oResponse.ReturnCode = "Error"
                    oResponse.Message = ex.Message
                End Try

    Error:

    {"Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Collage.Domain.Models.Order.Order'."}

    Friday, October 11, 2019 3:33 PM
  • Check this:

      _Order = JsonConvert.DeserializeObject(Of Domain.Models.Order.Order)( _JSON )

    • Marked as answer by old_School Monday, October 14, 2019 1:07 PM
    Friday, October 11, 2019 4:10 PM
  • Have you resolved the following thread?

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/7ffdc145-39fa-47cb-b2ff-b18379de721f/for-each-loop-help?forum=vbgeneral#7ffdc145-39fa-47cb-b2ff-b18379de721f


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, October 11, 2019 4:13 PM
    Moderator