java gson序列化,JavaScript无法解析用Java从Gson序列化的字符串

This is not a duplicate of this question as the question here is specifically about representation of JSON strings serialized from Java in literal form in JavaScript (and it's more than double quotes one has to worry about).

I am creating in Java a serialized JSON object using Gson which I am subsequently unable to internalize from Javascript using JSON.parse. This occurs when I have escaped double quotes in my Java object. I find that baffling as Gson is supposed to take care of proper escaping and the JSON serialization format is language-agnostic so when I serialize from Java I should be able to deserialize from JavaScript without any headaches.

Minimal code below:

Java serialization

import java.util.*;

import java.lang.reflect.Type;

import com.google.gson.Gson;

import com.google.gson.GsonBuilder;

class LOV {

List values;

public LOV(List values) {

this.values = values;

}

}

public class FooMain {

private static final Gson gson = new GsonBuilder().serializeNulls().create();

@SuppressWarnings("unchecked")

public static T fromJson(String json, Class classOfT) {

T target = (T) gson.fromJson(json, (Type) classOfT);

return target;

}

public static void main(String args[]) {

List values = new ArrayList<>();

values.add("\"foo\"");

LOV lov = new LOV(values);

String s = gson.toJson(lov);

System.out.printf("Stringified JSON is: [%s]\n", s);

LOV lov2 = fromJson(s, LOV.class);

}

}

… the above code shows that serialization and deserialization is possible and works. I also wrote an equals method for the LOV class to establish that the re-internalized object (lov2) is equal to the originally serialized one (and it is, I am just skipping this code to keep this short).

The above code prints on the console:

Stringified JSON is: [{"values":["\"foo\""]}]

JavaScript internalization

Yet, when I am trying to internalize in JavaScript the string created by Java:

JSON.parse('{"values":["\"foo\""]}');

… I get:

Uncaught SyntaxError: Unexpected token f in JSON at position 13(…)

Resolution

In the end, based on the accepted answer and the commentary to it I automatically escaped a number of characters in the Java side but there were many cases to consider. So I had to do (in Java) all of the following:

s.replace("\\", "\\\\")

s.replace("\"", "\\\"")

s.replace("'", "\\'")

… and on the JavaScript side I also had to do:

JSON.parse('the string literal prepared by Java'.replace(/\t/g, "\\t"));

This is working but is getting complicated and I wasn't sure I had all the cases covered so in the end I just used StringEscapeUtils.escapeEcmaScript and it worked like a charm. Using escapeEcmaScript a simple JSON.parse('the string literal prepared by Java') was sufficient on the JavaScript side.

解决方案

You need to use double backslash for Javascript's JSON.parse. This should work:

JSON.parse('{"values":["\\"foo\\""]}');

This is because you also need to escape the backslash.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值