JSONObject contains escape characters

江枫思渺然 提交于 2019-12-04 02:07:54

问题


I'm building a simulator to post JSON data to a service I'm running.

The JSON should look like this:

{"sensor":
       {"id":"SENSOR1","name":"SENSOR","type":"Temperature","value":100.12,"lastDateValue":"\/Date(1382459367723)\/"}
}

I tried this with the "Advanced REST Client" in Chrome and this works fine. The date get's parsed properly by the ServiceStack webservice.

So, the point is to write a sensor simulator that posts data like this to the web service.

I created this in Java, so I could run it on my raspberry pi.

This is the code:

    public static void main(String[] args) {

    String url = "http://localhost:63003/api/sensors";
    String sensorname = "Simulated sensor";
    int currentTemp = 10;
    String dateString = "\\" + "/Date(" + System.currentTimeMillis() + ")\\" + "/";
    System.out.println(dateString);

    System.out.println("I'm going to post some data to: " + url);

    //Creating the JSON Object
    JSONObject data = new JSONObject();
    data.put("id", sensorname);
    data.put("name", sensorname);
    data.put("type", "Temperature");
    data.put("value", currentTemp);
    data.put("lastDateValue", dateString);
    JSONObject sensor = new JSONObject().put("sensor",  data);

    //Print out the data to be sent
    StringWriter out = new StringWriter();
    sensor.write(out);

    String jsonText = out.toString();
    System.out.print(jsonText);

    //Sending the object
    HttpClient c = new DefaultHttpClient();
    HttpPost p = new HttpPost(url);
    p.setEntity(new StringEntity(sensor.toString(), ContentType.create("application/json")));

    try {
        HttpResponse r = c.execute(p);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

The output of this program is as follows:

\/Date(1382459367723)\/
I'm going to post some data to: http://localhost:63003/api/sensors
{"sensor":{"lastDateValue":"\\/Date(1382459367723)\\/","id":"Simulated sensor","name":"Simulated sensor","value":10,"type":"Temperature"}}

The issue here is that the JSONObject string still contains these escape characters. But when I print the string in the beginning it does not contain the escape characters. Is there any way to get rid of these? My service can't parse these..

This is a sample of what I send with the rest client in chrome:

 {"sensor":{"id":"I too, am a sensor!","name":"Willy","type":"Temperature","value":100.12,"lastDateValue":"\/Date(1382459367723)\/"}}

回答1:


JSONObject is correctly encoding the string. This page describes how string literals are to be escaped in JavaScript (and, by extension, JSON). The following note is important to understanding what happens in your example:

For characters not listed in Table 2.1, a preceding backslash is ignored, but this usage is deprecated and should be avoided.

Your example ("\/Date(1382459367723)\/") uses a preceding backslash before a /. Because / is not in table 2.1, the \ should simply be ignored. If your service doesn't ignore the \, then it either has a bug, or is not a JSON parser (perhaps it uses a data format which is similar to, but not quite, JSON).

Since you need to generate non-conforming JSON, you won't be able to use standard tools to do so. Your two options are to write your own not-quite-JSON encoder, or to avoid characters which must be escaped, such as \ and ".




回答2:


@pburka is correct. If you want to send it in \/Date(1382459367723)\/ format, try escaping the blackslash twice as below

String dateString = "\\\\" + "/Date(" + System.currentTimeMillis() + ")\\\\" + "/";

In the first pass, dateString will make it as \\/Date(1382459367723)\\/ and finally JSONObject will add extra backslashes internally to it's buffer i.e \\\/Date(1382459367723)\\\/ so that the blackslashes before / will be ignored according to JSON parsing rules and you would get the desired result i.e \/Date(1382459367723)\/



来源:https://stackoverflow.com/questions/19609205/jsonobject-contains-escape-characters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!