我想使用row_to_json(row)
函数直接在数据库中创建JSON对象。这按预期工作:
select row_to_json(country) from country
它返回类似:
[
{"identifier": 1234, "name": "US"},
{"identifier": 5678, "name": "France"},
....
]
但是,手动创建行时,我会丢失列名。例如,以下查询:
select row_to_json(row(country.identifier, country.name)) from country
给出:
[
{"f1": 1234, "f2": "US"},
{"f1": 5678, "f2": "France"},
....
]
此构造失去列名。我的输出中需要这些列名。
我找到了一个解决方案:
with country as (select identifier, name from country)
select row_to_json(country) from country
有没有办法直接这样做 - 也就是说,没有CTE?
答案 0 :(得分:1)
您可以使用派生表:
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top" ng-show="ngCart.getTotalItems()" class="ng-pristine ng-valid">
<input type="hidden" name="cmd" value="_xclick" autocomplete="off">
<input type="hidden" name="business" value="paypal_email" autocomplete="off">
<input type="hidden" name="lc" value="CA" autocomplete="off">
<input type="hidden" name="item_name" value="" autocomplete="off">
<input type="hidden" name="item_number" value="123" autocomplete="off">
<input type="hidden" name="amount" value="5.7" autocomplete="off">
<input type="hidden" name="currency_code" value="GBP" autocomplete="off">
<input type="hidden" name="button_subtype" value="services" autocomplete="off">
<input type="hidden" name="no_note" value="" autocomplete="off">
<input type="hidden" name="tax_rate" value="20" autocomplete="off">
<input type="hidden" name="shipping" value="2.5" autocomplete="off">
<input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest" autocomplete="off">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
或者您可以使用json_build_object(),但这需要重复列名称:
select row_to_json(t)
from (
select identifier, name
from country
) t