gpt4 book ai didi

java - 当值为日期时,如何使用 Java 在 JSON 字符串中添加引号

转载 作者:行者123 更新时间:2023-11-30 01:55:45 24 4
gpt4 key购买 nike

我在需要读取 Java 中的 JSON 对象的场景中遇到困难,该对象的键中没有双引号,也没有值,如下例所示:

"{id: 267107086801, productCode: 02-671070868, lastUpdate: 2018-07-15, lastUpdateTimestamp: 2018-07-15 01:49:58, user: {pf: {document: 123456789, name: Luis Fernando}, address: {street: Rua Pref. Josu00e9 Alves Lima,number:37}, payment: [{sequential: 1, id: CREDIT_CARD, value: 188, installments: 9}]}"

我可以使用下面的代码以及 replaceAllGson 库在字段中添加双引号:

String jsonString = gson.toJson (obj);

String jsonString = jsonString.replaceAll ("([\\ w] +) [] *:", "\" $ 1 \ ":"); // to quote before: value
jsonString = jsonString.replaceAll (": [] * ([\\ w @ \\.] +)", ": \" $ 1 \ ""); // to quote after: value, add special character as needed to the exclusion list in regex
jsonString = jsonString.replaceAll (": [] * \" ([\\ d] +) \ "", ": $ 1"); // to un-quote decimal value
jsonString = jsonString.replaceAll ("\" true \ "", "true"); // to un-quote boolean
jsonString = jsonString.replaceAll ("\" false \ "", "false"); // to un-quote boolean

但是,带有日期的字段被错误地分解,例如:

"{"id" : 267107086801,"productCode" : 02-671070868,"lastUpdate" : 2018-07-15,"lastUpdateTimestamp" : 2018-07-15 "01" : 49 : 58,"user" :{"pf":{"document" : 123456789, "name" : "Luis" Fernando},"address" :{"street" : "Rua"Pref.Josu00e9AlvesLima,"number" : 37},"payment" : [{"sequential" : 1,"id" : "CREDIT_CARD","value" : 188,"installments" : 9}]}"

此外,带有空格的字符串也是错误的。我该如何纠正这个逻辑?我究竟做错了什么?提前致谢。

最佳答案

    String incorrectJson = "{id: 267107086801, productCode: 02-671070868,"
+ " lastUpdate: 2018-07-15, lastUpdateTimestamp: 2018-07-15 01:49:58,"
+ " user: {pf: {document: 123456789, name: Luis Fernando},"
+ " address: {street: Rua Pref. Josu00e9 Alves Lima,number:37},"
+ " payment: [{sequential: 1, id: CREDIT_CARD, value: 188, installments: 9}]}";

String correctJson = incorrectJson.replaceAll("(?<=: ?)(?![ \\{\\[])(.+?)(?=,|})", "\"$1\"");
System.out.println(correctJson);

输出:

{id: "267107086801", productCode: "02-671070868", lastUpdate: "2018-07-15", lastUpdateTimestamp: "2018-07-15 01:49:58", user: {pf: {document: "123456789", name: "Luis Fernando"}, address: {street: "Rua Pref. Josu00e9 Alves Lima",number:"37"}, payment: [{sequential: "1", id: "CREDIT_CARD", value: "188", installments: "9"}]}

重要正则表达式的一个缺点是它们可能难以阅读。我在这里使用的匹配每个文字值(但不匹配对象或数组的值)。我使用冒号、逗号和大括号来指导匹配,因此我不需要关心每个字符串值内的内容,它可以是任何字符(逗号或右大括号除外)。各部分含义:

  • (?<=: ?) :值之前有一个冒号和一个可选的空格(lookbehind)
  • (?![ \\{\\[]) 该值以空格、大括号或方括号开头(负向先行;空白是因为我们不希望冒号和值之间有空格)值)
  • (.+?) :该值至少由一个字符组成,尽可能少(不情愿的量词;或者正则表达式会尝试获取字符串的其余部分)
  • (?=,|}) :值后面是逗号或右大括号(正向前瞻)。

如果不精通 JSON,我认为您不需要引用该名称。不过,您可以:

    String correctJson = incorrectJson.replaceAll(
"(?<=\\{|, ?)([a-zA-Z]+?): ?(?![ \\{\\[])(.+?)(?=,|})", "\"$1\": \"$2\"");

{"id": "267107086801", "productCode": "02-671070868", "lastUpdate": "2018-07-15", "lastUpdateTimestamp": "2018-07-15 01:49:58", user: {pf: {"document": "123456789", "name": "Luis Fernando"}, address: {"street": "Rua Pref. Josu00e9 Alves Lima","number": "37"}, payment: [{"sequential": "1", "id": "CREDIT_CARD", "value": "188", "installments": "9"}]}

关于java - 当值为日期时,如何使用 Java 在 JSON 字符串中添加引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54584696/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com