简体   繁体   中英

Saving Json into DB with Spring framework

I save Json String into DB:

    for (int i = 0; i < callSubSetJson.size(); i++) {
        CallDto c = new CallDto();
        c.setCallsJ(callSubSetJson.get(i));
        _callRepository.save(c);
    }

and it is saved in format like this:

[{\\"number\\":\\"0005\\",\\"name\\":\\"whoewer :)\\",\\"id\\":3377,\\"date\\":\\"14385113015...etc

when I return that to client I have to do some clean up, remove all the \\ .

I believe that this is not the most efficient way to do it. What is the correct and efficient way to do it?

Use JsonEncoder.encode(result); this will remove all .

If you are going to use this you need the json-simple library (I used version 1.1.1 ); and this is how it may look like:

final String data = "[{\"number\":\"0005\",\"name\":\"whoewer :)\",\"id\":3377,\"date\":\"14385113015\"}]";
final Object obj = JSONValue.parse(data);
final JSONArray array = (JSONArray)obj;

System.out.printf("%s%n", array);

Output

[{"date":"14385113015","number":"0005","name":"whoewer :)","id":3377}]

You have various choices to d so. Yoi can either use JSONENCODE, StringTokenize or any regex to do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM