JSONObject包含转义字符 [英] JSONObject contains escape characters

查看:2065
本文介绍了JSONObject包含转义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个模拟器,将JSON数据发布到我正在运行的服务中。

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

JSON应如下所示:

The JSON should look like this:

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

我在Chrome中使用高级REST客户端尝试了这个,这很好用。 ServiceStack Web服务正确解析了日期。

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

因此,重点是编写一个传感器模拟器,将这样的数据发布到Web服务。

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

我是用Java创建的,所以我可以在我的树莓派上运行它。

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

这是代码:

    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();
    }
}

此程序的输出如下:

\/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"}}

这里的问题是JSONObject字符串仍然包含这些转义字符。但是当我在开头打印字符串时,它不包含转义字符。有没有办法摆脱这些?我的服务无法解析这些..

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..

这是我用chrome发送给其余客户端的示例:

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)\/"}}


推荐答案

JSONObject 正确编码字符串。 此页面描述了如何在JavaScript(以及扩展名为JSON)中转义字符串文字。以下注释对于理解示例中发生的情况非常重要:

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:


对于表2.1中未列出的字符,将忽略前面的反斜杠,但此用法已弃用,应避免使用。

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

您的示例(\ / Date(1382459367723)\\ \\ /)在 / 之前使用前面的反斜杠。由于 / 不在表2.1中,因此应该忽略 \ 。如果您的服务没有忽略 \ ,那么它有一个错误,或者不是JSON解析器(也许它使用的数据格式类似于,但是不完全是,JSON)。

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).

由于您需要生成不符合要求的JSON,您将无法使用标准工具来执行此操作。您的两个选项是编写自己的不完全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 ".

这篇关于JSONObject包含转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆