java jsonobject 转义字符,JSONObject包含转义字符

博主在Java中创建了一个传感器模拟器,用于向服务端POST JSON数据,但遇到了JSON字符串包含转义字符的问题。在Chrome的Advanced REST Client中发送的数据能被正确解析,而在Java程序中生成的JSON由于包含了转义斜杠导致服务端无法解析。解决方案是理解JSON编码规则,服务端可能需要兼容非标准的JSON格式或者调整编码方式以避免特殊字符。
摘要由CSDN通过智能技术生成

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

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值