Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6607497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:30:35+00:00 2026-05-25T19:30:35+00:00

I have a .net Webservice which should communicate with a Java app via json.

  • 0

I have a .net Webservice which should communicate with a Java app via json.

Now I have a method on the server side that looks like this:

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public DateTime GetDate(DateTime input)
    {
        return input;
    }

from a C# app I can send a receive DateTime value. It’s a convention that Date values are serialized as:

\/Date(1279176056000)\/

where the number is defined as seconds since epoch.
So if I want to call that service my json request string must look like this:

{"input":"\/Date(1279176056000)\/"}

Howevery, I don’t know how to achive that with the json.org.* classes on the Java side.
The problem: If I use this code:

JSONObject json = new JSONObject();
json.put("input", "\\/Date(1279176056000)\\/");

the JSONObject is smart enough to escape the string itself before sending it through the wire, so I get:

{"input":"\\/Date(1279176056000)\\/"}

which results in an Exception during server side deserialisation:

System.FormatException: \/Date(1279183256000)\/ is not a valid value for DateTime
bei System.ComponentModel.DateTimeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
bei System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
bei System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject)
bei System.Web.Script.Serialization.ObjectConverter.ConvertObjectToType(Object o, Type type, JavaScriptSerializer serializer)
bei System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary`2 rawParams)
bei System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)
bei System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)
bei System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)

Long story short: How can I pass a backslash as a parameter to JSONObject without having it escaped?

Well, you might think I just just build the JSON string myself but I really want to send and receive more complex objects/arrays containing Date properties and I don’t want to handle the whole JSON generation myself.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:30:35+00:00Added an answer on May 25, 2026 at 7:30 pm

    Here is my own solution:

    import java.util.Date;
    
    public class DateConverter {
    
        private static long TicksToMillisOffset = 621355968000000000L;
        private static long TicksPerMillisecond = 10000L;
    
        public static Long toTicks(Date date)
        {
            if (date == null) return null;
    
            int offset = date.getTimezoneOffset() * 60;
            long ms = date.getTime();
    
            return (ms + offset) * TicksPerMillisecond + TicksToMillisOffset;   
        }
    
        public static Date fromTicks(Long ticks)
        {
            return ticks == null 
                ? null
                : new Date((ticks - TicksToMillisOffset) / TicksPerMillisecond);
        }
    
        public static String toJSONString(Date date) {
            return date != null ? date.toGMTString() : null;
        }
    
        public static Date fromJSONString(String string) {
    
            // expected: "/Date(secondssinceepoch)/"
            if (string.matches("^/Date\\(\\d+\\)/$")) {
                String value = string.replaceAll("^/Date\\((\\d+)\\)/$", "$1");
                return new Date(Long.valueOf(value));
            }
            else {
                return new Date(Date.parse(string));
            }
    
        }
    
    }
    

    usage:

    // deserialisation:
    JSONObject json = GetJSONObjectFromNetWebService();
    
    Poco item = new Poco();
    item.id = json.getInt("Id");
    item.name = json.GetString("Name");
    item.dateValue = DateConverter.fromJSONString(json.getString("DateValue"));
    
    // serialisation
    JSONObject json = new JSONObject();
    
    Poco item = GetPocoFromSomeWhereElse();
    json.put("Id", item.id);
    json.put("Name", item.name);
    json.put("DateValue", DateConverter.toJSONString(item.dateValue));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.Net-Button which, when clicked, executes client side and server side code.
I have a webservice programmed in coldfusion which I'm attempting to consume using c#.net.
I have a medium sized application that runs as a .net web-service which I
I have an ASP.net web service that I'm using for a web application which
I have an ASP.NET WebService that returns an object of List public class Students
I have a asp.net webservice running in windows server 2003, i want it read/write
So I have a Stateful .NET webservice (C#) that I would like Flex to
Hy, I'm developing a Grails app which has to communicate with an existing Java
We have a Java WebService which is sending messages(XML file with a set of
I'm developing an asp.net webservice application to provide json-formatted data to a widget that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.