我有一个表,该表的字符串列包含如下值:“ ID:1,名称:john doe,职业:推销员”。我想将其转换为这样的JSON对象列:{“ ID”:“ 1”,“ name”:“ john doe”,“ occupation”:“ salesmen”}
目前,我的解决方案是:
<div class="uploadImage-wrap">
<!-- Comment Title -->
<div class="uploadImageTitle-wrap">
<h2>Upload Files</h2>
</div>
<div id="uploadImage-containerSEC-2">
<div id="dropzoneplaceSEC-2" class="dz-message">
<div class="needsclick">
<i class="fa fa-upload" aria-hidden="true"></i></br>
Drop files here to upload.<br> or browse for a file
</div>
</div>
<input name="userFileName" type="hidden" value="" id="userFileNameSEC-2">
<input name="issueKey" type="hidden" value="" id="issueKeySEC-2">
<a href="#"><button type="button" id="uploadImageButtonSEC-2" class="btn blue changeBtn display-none" style='margin-left:40%;' onclick="addAttachmentForIssue(this)">Upload</button></a>
</div>
</div><br/>
问题在于字符串实际上包含将近100个键-值对,并且该表具有数百万行,因此使用regex_split_to_table将使该表爆炸。在Postgresql中有什么有效的方法吗?
答案 0 :(得分:0)
您在这里不一定需要正则表达式函数,例如:
db=# with c as (select unnest('{ID: 1, name: john doe, occupation: salesmen}'::text[]))
select string_to_array(unnest,': ') from c;
string_to_array
-----------------------
{ID,1}
{name,"john doe"}
{occupation,salesmen}
(3 rows)
不确定什么会更快。
关于内置json格式-我认为您必须提供以太行或格式为JSON
的文件-当前没有可用的解析器...