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

在需要读取 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}]}"

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


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}]}"

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


宝慕林4294392
浏览 342回答 2
2回答

哔哔one

&nbsp; &nbsp;String incorrectJson = "{id: 267107086801, productCode: 02-671070868,"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + " lastUpdate: 2018-07-15, lastUpdateTimestamp: 2018-07-15 01:49:58,"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + " user: {pf: {document: 123456789, name: Luis Fernando},"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + " address: {street: Rua Pref. Josu00e9 Alves Lima,number:37},"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + " payment: [{sequential: 1, id: CREDIT_CARD, value: 188, installments: 9}]}";&nbsp; &nbsp; String correctJson = incorrectJson.replaceAll("(?<=: ?)(?![ \\{\\[])(.+?)(?=,|})", "\"$1\"");&nbsp; &nbsp; System.out.println(correctJson);输出:{id:“267107086801”,productCode:“02-671070868”,lastUpdate:“2018-07-15”,lastUpdateTimestamp:“2018-07-15 01:49:58”,用户:{pf:{document:“123456789 ",姓名:"Luis Fernando"},地址:{街道:"Rua Pref. Josu00e9 Alves Lima",编号:"37"},付款:[{sequential:"1",id:"CREDIT_CARD",值:" 188”,分期付款:“9”}]}非平凡的正则表达式的一个缺点是它们很难阅读。我在这里使用的匹配每个文字值(但不匹配对象或数组的值)。我使用冒号、逗号和大括号来指导匹配,所以我不需要关心每个字符串值内部的内容,它可以是任何字符(逗号或右大括号除外)。零件的意思是:(?<=: ?):在值之前有一个冒号和一个可选的空格(后视)(?![ \\{\\[])该值不以空白、花括号或方括号开头(负前瞻;空白,因为我们不希望冒号和值之间有空白作为值的一部分)(.+?):该值由至少一个字符组成,尽可能少(不情愿的量词;或者正则表达式会尝试获取字符串的其余部分)(?=,|}): 值后是逗号或右花括号(正向前瞻)。如果不精通 JSON,我认为您不需要引用名称。不过,您可以:&nbsp; &nbsp; String correctJson = incorrectJson.replaceAll(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "(?<=\\{|, ?)([a-zA-Z]+?): ?(?![ \\{\\[])(.+?)(?=,|})", "\"$1\": \"$2\"");{“id”:“267107086801”,“productCode”:“02-671070868”,“lastUpdate”:“2018-07-15”,“lastUpdateTimestamp”:“2018-07-15 01:49:58”,用户: {pf:{“document”:“123456789”,“name”:“Luis Fernando”},地址:{“street”:“Rua Pref. Josu00e9 Alves Lima”,“number”:“37”},付款:[ {“顺序”:“1”,“id”:“CREDIT_CARD”,“价值”:“188”,“分期付款”:“9”}]}

交互式爱情

以下代码注意 JSON 字符串中存在的单引号以及包含数字的键jsonString = jsonString.replaceAll(":",":");&nbsp;// 键后跳空格 jsonString = jsonString.replaceAll(": ,",":,");&nbsp;jsonString = jsonString.replaceAll("(?<=: ?)(?![ \{\[])(.+?)(?=,|})", ""$1"");&nbsp;jsonString = jsonString.replaceAll("(?<=\{|, ?)([a-zA-Z0-9]+?)(?=:)",""$1"");&nbsp;jsonString = jsonString.replaceAll(""true"", "true");&nbsp;// 取消引用 boolean jsonString = jsonString.replaceAll(""false"", "false");&nbsp;// 取消引用 boolean jsonString = jsonString.replaceAll(""null"", "null");// 取消引用 null jsonString = jsonString.replaceAll(":",", ":"" ,) ; // 删除不必要的双引号 jsonString = jsonString.&nbsp;replaceAll("true"", "true"); // 取消引用 boolean jsonString = jsonString.replaceAll("'",", "',");&nbsp;// 处理 json 字符串中的单引号 jsonString = jsonString.replaceAll("'},", "'}","); // 将双引号放在以单引号结尾的字符串之后
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java