locked
Write a generic handler or webservice to handle nested JSON RRS feed

  • Question

  • User-1305530094 posted

    I have the following JSON and I want to write a generic handler or pagemethod to process this data to sql server. I m stuck here and can't figure it out. please if you could help

    how can I map through it. I know that I need a loop but again can't figure it out

    I m using ASP/vb.net

    {
    	"action": "edit",
    	"data": {
    		"2383": {
    			"RecID": 2383,
    			"PtFilenum": "15090248",
    			"PrtFilenum": 13090701,
    			"FullName": "saied salem",
    			"DOB": "04 Oct 1985",
    			"PrtStatus": ""
    		},
    		"3387": {
    			"RecID": 3387,
    			"PtFilenum": "15090248",
    			"PrtFilenum": 15120996,
    			"FullName": "marwam mohmmad  saleem",
    			"DOB": "24 May 2017",
    			"PrtStatus": ""
    		}
    	}
    }

    Friday, May 26, 2017 6:50 AM

Answers

  • User475983607 posted

    Fix the JSON. You have objects that are named by an ID.  It will be very difficult to deserialize as you will need a class for every record in the DB... or you'll need to reflect over the objects and convert them manually.

    {
    	"action": "edit",
    	"data": [{
    			"RecID": 2383,
    			"PtFilenum": "15090248",
    			"PrtFilenum": 13090701,
    			"FullName": "saied salem",
    			"DOB": "04 Oct 1985",
    			"PrtStatus": ""
    		},
    		{
    			"RecID": 3387,
    			"PtFilenum": "15090248",
    			"PrtFilenum": 15120996,
    			"FullName": "marwam mohmmad  saleem",
    			"DOB": "24 May 2017",
    			"PrtStatus": ""
    		}]
    }

    Then all you have to do is craft an VB.NET class that matches the JSON shape

    Public Class EditData
        Public Property action As String
        Public Property data As FileRecord()
    
    End Class
    
    Public Class FileRecord
        Public Property RecID As Integer
        Public Property PtFilenum As String
        Public Property PrtFilenum As Integer
        Public Property FullName As String
        Public Property DOB As String
        Public Property PrtStatus As String
    End Class

    Now you can simply pass the EditData type and the .NET serializer will handle the rest.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 26, 2017 10:37 AM
  • User475983607 posted

    There is no need to manually serialize the object as the .NET framework will serialize the types automatically.   When you manually serialize the data you end up serializing the data twice.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 26, 2017 5:52 PM

All replies

  • User475983607 posted

    Fix the JSON. You have objects that are named by an ID.  It will be very difficult to deserialize as you will need a class for every record in the DB... or you'll need to reflect over the objects and convert them manually.

    {
    	"action": "edit",
    	"data": [{
    			"RecID": 2383,
    			"PtFilenum": "15090248",
    			"PrtFilenum": 13090701,
    			"FullName": "saied salem",
    			"DOB": "04 Oct 1985",
    			"PrtStatus": ""
    		},
    		{
    			"RecID": 3387,
    			"PtFilenum": "15090248",
    			"PrtFilenum": 15120996,
    			"FullName": "marwam mohmmad  saleem",
    			"DOB": "24 May 2017",
    			"PrtStatus": ""
    		}]
    }

    Then all you have to do is craft an VB.NET class that matches the JSON shape

    Public Class EditData
        Public Property action As String
        Public Property data As FileRecord()
    
    End Class
    
    Public Class FileRecord
        Public Property RecID As Integer
        Public Property PtFilenum As String
        Public Property PrtFilenum As Integer
        Public Property FullName As String
        Public Property DOB As String
        Public Property PrtStatus As String
    End Class

    Now you can simply pass the EditData type and the .NET serializer will handle the rest.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 26, 2017 10:37 AM
  • User767034699 posted

    HI there Embryologist,

    so you want to wrie a handler based service correct?

    first need to import these libraries using nuget

    jayrock
    jayrock-json
    System.Runtime.Serialization.Json

    and then reference them in your handler file like

    using Jayrock.Json; //vb import
    using Jayrock.JsonRpc;
    using Jayrock.JsonRpc.Web;

    then you will have a method lets take for a example

    [Jayrock.JsonRpc.JsonRpcMethod("UploadcustomerOrders")] exposing this...
    public bool UploadcustomerOrders(object[] records)

    {

    return true/false after inserting else update

    false for any errors during the inserting to sql server

    }

    Public Function UploadcustomerOrders() As Boolean
    
    End Function

    ofcause this will accept object array[]

    kind regards

    Tony

    Friday, May 26, 2017 5:25 PM
  • User475983607 posted

    There is no need to manually serialize the object as the .NET framework will serialize the types automatically.   When you manually serialize the data you end up serializing the data twice.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 26, 2017 5:52 PM
  • User-1305530094 posted

    thank u for the generous explanation. I would be really thankful if you can guid me one more time I m really new to generic handlers and I prefer to do it via page method

    my  vb sql data old code is below

    <WebMethod()> _
        Public Shared Function TblRegJoinUpd(ByVal Ptfn As String, ByVal Prfnarray As String, ByVal PrfStarray As String)
            Dim constr As String = ConfigurationManager.ConnectionStrings("ARTSQLConStrng").ConnectionString
            Using con As New SqlConnection(constr)
                Using cmd As New SqlCommand("TblRegjoinUpdRec", con)
                    cmd.CommandType = CommandType.StoredProcedure
                    Dim sptfnArray = Prfnarray.Split(",")
                    Dim sPrfStarray = PrfStarray.Split(",")
                    con.Open()
                    For i As Integer = 0 To sPrfStarray.Length - 1
                        cmd.Parameters.Clear()
                        cmd.Parameters.Add("@PtFileNum", SqlDbType.Int).Value = Convert.ToInt32(Ptfn)
                        cmd.Parameters.Add("@PrtFileNum", SqlDbType.Int).Value = Convert.ToInt32(sptfnArray(i))
                        cmd.Parameters.Add("@PrtStatus", SqlDbType.Bit).Value = sPrfStarray(i)
                        cmd.ExecuteNonQuery()
                    Next
                    con.Close()
                End Using
            End Using
            Return "Success"

    so basically i will be needing just the following classes, because the rest of the data in JSON is just readonly, sorry for that mistake

    Public Class EditData
        Public Property action As String
        Public Property data As FileRecord()
    
    End Class
    
    Public Class FileRecord
        Public Property RecID As Integer
        Public Property PtFilenum As String
        Public Property PrtFilenum As Integer
        Public Property PrtStatus As String
    End Class

    how shall I funnel my JSON string through page method, if it does not work, how to handle it through a generic handler

    I just have a trial below! I know its poor but i m pretty new to this method

    Public Class updatehdlr
        Implements System.Web.IHttpHandler
    Public Class EditData
    Public Property action As String
        Public Property data As FileRecord()
    
    End Class
    
    Public Class FileRecord
        Public Property RecID As Integer
        Public Property PtFilenum As String
        Public Property PrtFilenum As Integer
        Public Property PrtStatus As String
    End Class
    Public Sub ProcessRequest(context As HttpContext) Implements IHttpHandler.ProcessRequest
    context.Response.ContentType = "JSON" Dim stringParam As String = DirectCast(context.Request("RecID"), String)..........

    Friday, May 26, 2017 9:49 PM