Results 1 to 2 of 2

Thread: How can i return json without decimal points

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2020
    Location
    Kampala
    Posts
    9

    How can i return json without decimal points

    Code:
    Imports System.Net.Http.Formatting
    Imports System.Net.Http.Headers
    
    Public Class CustomJSONFormatter
        Inherits JsonMediaTypeFormatter
        Public Sub New()
    
            Me.SupportedMediaTypes.Add(New MediaTypeHeaderValue("text/html"))
        End Sub
        Public Overrides Sub SetDefaultContentHeaders(type As Type, headers As HttpContentHeaders, mediaType As MediaTypeHeaderValue)
            MyBase.SetDefaultContentHeaders(type, headers, mediaType)
            headers.ContentType = New MediaTypeHeaderValue("application/json")
        End Sub
    End Class
    Code:
    Imports System.Net.Http
    Imports System.Web.Http
    Imports Microsoft.Owin.Security.OAuth
    Imports Newtonsoft.Json.Serialization
    
    Public Module WebApiConfig
        Public Sub Register(config As HttpConfiguration)
          ' Web API configuration and services
          ' Configure Web API to use only bearer token authentication.
          config.SuppressDefaultHostAuthentication()
          config.Filters.Add(New HostAuthenticationFilter(OAuthDefaults.AuthenticationType))
    
            ' Web API routes
            config.MapHttpAttributeRoutes()
    
            config.Routes.MapHttpRoute(
                name:="DefaultApi",
                routeTemplate:="api/{controller}/{id}",
                defaults:=New With {.id = RouteParameter.Optional}
            )
            config.Formatters.Add(New CustomJSONFormatter())
            ' Adding formatter for XML   
            ' config.Formatters.XmlFormatter.MediaTypeMappings.Add(New QueryStringMapping("type", "xml", New MediaTypeHeaderValue("application/xml")))
        End Sub
    End Module
    Below is the retun json data but with decimal points how can i create a customised formatter for this
    Code:
    [{"Account":"19-07561","Names":"MAKUMBI TARIQ AZIZI","Stream":"F","Class":"P1","Expressed":"MID TERM 1 2019","sstscore":64.0,"sstgrade":5.0,"engscore":75.0,"engrade":3.0,"mathsscore":49.0,"mathsgrade":8.0,"sciencescore":63.0,"sciencegrade":5.0,"totalscore":251.0,"Totalgrade":21.0,"Division":2.0,"ire":"","iregrade":"","Years":"2019","atk":1},{"Account":"19-07561","Names":"MAKUMBI TARIQ AZIZI","Stream":"F","Class":"P1","Expressed":"END TERM 1 2019","sstscore":70.0,"sstgrade":3.0,"engscore":70.0,"engrade":3.0,"mathsscore":60.0,"mathsgrade":5.0,"sciencescore":68.0,"sciencegrade":4.0,"totalscore":268.0,"Totalgrade":15.0,"Division":2.0,"ire":"","iregrade":"","Years":"2019","atk":2},{"Account":"19-07561","Names":"MAKUMBI TARIQ AZIZI","Stream":"F","Class":"P1","Expressed":"MID TERM2 2019","sstscore":75.0,"sstgrade":3.0,"engscore":55.0,"engrade":6.0,"mathsscore":71.0,"mathsgrade":3.0,"sciencescore":71.0,"sciencegrade":3.0,"totalscore":272.0,"Totalgrade":15.0,"Division":2.0,"ire":"","iregrade":"","Years":"2019","atk":3},{"Account":"19-07561","Names":"MAKUMBI TARIQ AZIZI","Stream":"F","Class":"P1","Expressed":"END TERM 2 2019","sstscore":59.0,"sstgrade":6.0,"engscore":52.0,"engrade":7.0,"mathsscore":76.0,"mathsgrade":3.0,"sciencescore":61.0,"sciencegrade":5.0,"totalscore":248.0,"Totalgrade":21.0,"Division":2.0,"ire":"","iregrade":"","Years":"2019","atk":4},{"Account":"19-07561","Names":"MAKUMBI TARIQ AZIZI","Stream":"F","Class":"P1","Expressed":"END TERM 2 2019","sstscore":"","sstgrade":"","engscore":"","engrade":"","mathsscore":"","mathsgrade":"","sciencescore":"","sciencegrade":"","totalscore":0.0,"Totalgrade":0.0,"Division":2.0,"ire":"","iregrade":"","Years":"2019","atk":5},{"Account":"19-07561","Names":"MAKUMBI TARIQ AZIZI","Stream":"F","Class":"P1","Expressed":"MID TERM3 2019","sstscore":67.0,"sstgrade":4.0,"engscore":52.0,"engrade":7.0,"mathsscore":80.0,"mathsgrade":2.0,"sciencescore":56.0,"sciencegrade":6.0,"totalscore":255.0,"Totalgrade":19.0,"Division":2.0,"ire":"61","iregrade":"5","Years":"2019","atk":6},{"Account":"19-07561","Names":"MAKUMBI TARIQ AZIZI","Stream":"F","Class":"P1","Expressed":"MID TERM3 2019","sstscore":"","sstgrade":"","engscore":"","engrade":"","mathsscore":"","mathsgrade":"","sciencescore":"","sciencegrade":"","totalscore":0.0,"Totalgrade":0.0,"Division":2.0,"ire":"","iregrade":"","Years":"2019","atk":7},{"Account":"19-07561","Names":"MAKUMBI TARIQ AZIZI","Stream":"F","Class":"P1","Expressed":"END TERM 3 2019","sstscore":67.0,"sstgrade":4.0,"engscore":74.0,"engrade":3.0,"mathsscore":88.0,"mathsgrade":2.0,"sciencescore":60.0,"sciencegrade":5.0,"totalscore":289.0,"Totalgrade":14.0,"Division":2.0,"ire":"75","iregrade":"3","Years":"2019","atk":8},{"Account":"19-07561","Names":"MAKUMBI TARIQ AZIZI","Stream":"F","Class":"P1","Expressed":"END TERM 3 2019","sstscore":"","sstgrade":"","engscore":"","engrade":"","mathsscore":"","mathsgrade":"","sciencescore":"","sciencegrade":"","totalscore":0.0,"Totalgrade":0.0,"Division":2.0,"ire":"","iregrade":"","Years":"2019","atk":9},{"Account":"19-07561","Names":"MAKUMBI TARIQ AZIZI","Stream":"R","Class":"P2","Expressed":"MID TERM 1 2020","sstscore":58.0,"sstgrade":6.0,"engscore":57.0,"engrade":6.0,"mathsscore":83.0,"mathsgrade":2.0,"sciencescore":61.0,"sciencegrade":5.0,"totalscore":259.0,"Totalgrade":19.0,"Division":2.0,"ire":"40","iregrade":"8","Years":"2020","atk":10},{"Account":"19-07561","Names":"MAKUMBI TARIQ AZIZI","Stream":"R","Class":"P2","Expressed":"MID TERM 1 2020","sstscore":"","sstgrade":"","engscore":"","engrade":"","mathsscore":"","mathsgrade":"","sciencescore":"","sciencegrade":"","totalscore":0.0,"Totalgrade":0.0,"Division":2.0,"ire":"","iregrade":"","Years":"2020","atk":11},{"Account":"19-07561","Names":"MAKUMBI TARIQ AZIZI","Stream":"R","Class":"P2","Expressed":"END TERM 2 2020","sstscore":76.0,"sstgrade":3.0,"engscore":81.0,"engrade":2.0,"mathsscore":95.0,"mathsgrade":1.0,"sciencescore":76.0,"sciencegrade":3.0,"totalscore":328.0,"Totalgrade":9.0,"Division":1.0,"ire":"","iregrade":"","Years":"2020","atk":12},{"Account":"19-07561","Names":"MAKUMBI TARIQ AZIZI","Stream":"R","Class":"P2","Expressed":"END TERM 2 2020","sstscore":"","sstgrade":"","engscore":"","engrade":"","mathsscore":"","mathsgrade":"","sciencescore":"","sciencegrade":"","totalscore":0.0,"Totalgrade":0.0,"Division":1.0,"ire":"","iregrade":"","Years":"2020","atk":13}]

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    18,012

    Re: How can i return json without decimal points

    That data with decimal points can be "quoted" like the string values are. It is expecting an integer value for those unquoted numeric values.

    How do you control the "quoting" of some fields over others? Is that being determined automatically by the "data types" in the underlying query? Can you just cast those values as "varchar()" in the query?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width