将“格式错误"的Java json对象转换为javascript [英] converting 'malformed' java json object to javascript

查看:116
本文介绍了将“格式错误"的Java json对象转换为javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java JSON对象,其格式为[{a = b}],我正在尝试将此对象作为JSON对象传递到javascript中,但是在键和值上都缺少",并且缺少"=" "代替:"

I have a Java JSON Object, its format is [{a=b}], I am trying to pass this object into javascript as a JSON object but its missing " on both the key and value as well as having "=" instead of ":"

是否有一种简单的方法可以将此JAVA JSON对象转换为可通过不同服务使用的对象?

Is there a simple way of converting this JAVA JSON object to be consumable by different services?

事实证明,解析是非常复杂的,因为实际的JSON是嵌套的,并且缺少引号和嵌套指示.

Parsing is proving to be very complicated as the actual JSON is nested and the lack of quotations and the lacking of indications for nestedness.

"JSON"数据示例:

Sample of 'JSON' data:

[{wwnType = Virtual,serialNumberType = Virtual,connections = [],已修改= 2016-10-29T19:00:04.457Z,macType = Virtual,category = server-profile-templates,serverHardwareTypeUri =/rest/server- hardware-types/32006464-D3C6-4B4E-8328-47A193C6116C,bios = {overriddenSettings = [],manageBios = false},固件= {firmwareBaselineUri = null,manageFirmware = false,forceInstallFirmware = false,firmwareInstallType = null},boot = { manageBoot = true,顺序= [CD,软盘,USB,硬盘,PXE]},hideUnusedFlexNics = true,bootMode = null,state = null,affinity = Bay,localStorage = {controllers = []},type = ServerProfileTemplateV1,status = OK,description =,eTag = 1477767604457/1,serverProfileDescription = test,name = test,创建= 2016-10-29T19:00:04.428Z,enclosureGroupUri =/rest/enclosure-groups/e989621b-930e-40e7-9db0-a6ddbf841709 ,uri =/rest/server-profile-templates/db1dbdcc-4237-4452-acc3-cf9dfdc75365,sanStorage = {manageSanStorage = false,volumeAttachments = []}}]

[{wwnType=Virtual, serialNumberType=Virtual, connections=[], modified=2016-10-29T19:00:04.457Z, macType=Virtual, category=server-profile-templates, serverHardwareTypeUri=/rest/server-hardware-types/32006464-D3C6-4B4E-8328-47A193C6116C, bios={overriddenSettings=[], manageBios=false}, firmware={firmwareBaselineUri=null, manageFirmware=false, forceInstallFirmware=false, firmwareInstallType=null}, boot={manageBoot=true, order=[CD, Floppy, USB, HardDisk, PXE]}, hideUnusedFlexNics=true, bootMode=null, state=null, affinity=Bay, localStorage={controllers=[]}, type=ServerProfileTemplateV1, status=OK, description=, eTag=1477767604457/1, serverProfileDescription=test, name=test, created=2016-10-29T19:00:04.428Z, enclosureGroupUri=/rest/enclosure-groups/e989621b-930e-40e7-9db0-a6ddbf841709, uri=/rest/server-profile-templates/db1dbdcc-4237-4452-acc3-cf9dfdc75365, sanStorage={manageSanStorage=false, volumeAttachments=[]}}]

谢谢

推荐答案

这不会很简单.但是,我认为只要您愿意编写一个标记器或词法分析器以将输入字符串分解为标记,就可以在不编写成熟的解析器的情况下完成此操作.基本计划可能是这样的:

It's not going to be simple. However, I think you can do this without writing a full-fledged parser, as long as you're willing to write a tokenizer, or lexical analyzer, to break your input string into tokens. The basic plan could be something like:

  1. 将您的输入转换为令牌列表.我不知道您输入的格式是什么,因此您需要进行自己的分析.令牌将类似于单个字符[]{},逗号,=;或标识符(在您的示例中为ab,但我不知道可能的有效格式是什么);或者也许是带引号的字符串文字,或者是数字文字,具体取决于您的需求.

  1. Convert your input into a list of tokens. I don't know what the format of your input is, so you'll need to do your own analysis. A token would be something like a single character [, ], {, }, comma, =; or an identifier (a or b in your example, but I don't know what the possible valid formats are); or, maybe, a string literal in quotes, or a numeric literal, depending on what your needs are.

遍历字符串并替换所需的令牌.根据您的示例,我想说在{之后:如果在此之后的第一个标记是标识符,则将其放在引号中;如果第二个令牌之后是=,则将其更改为:;如果此后的第三个标记是标识符,则将其用引号引起来.逗号后也可能如此,但是您需要跟踪逗号是对象中键-值对列表的分隔符还是数组中值列表的分隔符.为此,您可能需要保留一个堆栈,在您看到[{时将其推入,并在看到}]时将其弹出,以使您知道自己是在对象内还是在对象内部.数组.

Go through the string and replace the tokens you need to. Based on your example, I'd say that after a {: if the first token after that is an identifier, put it in quotes; if the second token after that is =, change it to :; if the third token after that is an identifier, put it in quotes. The same could be true after a comma, but you'll need to keep track of whether the comma is a separator for a list of key-value pairs in an object, or a list of values in an array. For that, you may need to keep a stack that you push whenever you see [ or {, and pop whenever you see } or ], so that you know whether you're inside an object or an array.

完成所有内容的替换后,将令牌重新连接在一起.结果应该是格式正确的JSON对象.

After you're done replacing everything, concatenate the tokens back together. The result should be a well-formed JSON object.

这只是一个粗略的概述,因为我真的不知道您的所有要求.您可能必须调整此答案才能满足您的确切需求.但我希望这对您如何解决此问题有一个大致的了解.

This is just a rough outline, since I really don't know all your requirements. You'll probably have to adapt this answer to meet your exact needs. But I hope this helps as a general idea of how you could approach the problem.

对不起,我认为没有一个简单的答案,除了您可能想研究解析器生成器(请参阅

Sorry, I don't think there's a simpler answer, except that you might want to look into parser generators (see Yacc equivalent for Java). I haven't actually looked at any in Java, so I don't know how simple they are to use. Please don't try to solve the whole thing with regexes. (Regexes will be useful for breaking your string into tokens, but trying to do more than that with regexes is likely to produce nothing but migraine.)

这篇关于将“格式错误"的Java json对象转换为javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆