0

我有一些客户数据,我使用 php 脚本将它们从 CSV 转换为 json。

转换后的数据全部混杂在一起,如下所示:

[{"客户编号":"12345","客户名称":"客户 1","地址代码":"PRIMARY","销售人员 ID":"aa","送货地址 1":"12345", "Ship to Address 2":"123, fake street","Ship to Address 3":"","Ship to City":"Toronto","Ship to Province":"ONTARIO","Ship to Postal Code" :"A1A 1A1","国家代码":"CA","运送到国家":"加拿大","运送到联系人":"John Doe","运送到电话":"416-555-5555" ,"Ship to Fax":"416-555-5552","Shipping Method":"OUR TRUCK","Bill to Address 1":"12345","Bill to Address 2":"123, fake street","Bill to Address 3":"","Bill to City":"Toronto","Bill to Province":"ONTARIO","Bill to Postal Code":"A1A 1A1","Bill to Phone":"416-555-5555","Tax Schedule ID":"HST ON/S","Location Code":"TOR","Payment Terms ID":"30 DAYS PD CHEQUE","信用额度金额":"2,000.00","Comment1":"30D PDC /10/11-NSF=WR BK ACCT","Comment2":"","持有":"1","价目表":"EASTGEN"} ]Bill to Phone":"416-555-5555","Tax Schedule ID":"HST ON/S","Location Code":"TOR","Payment Terms ID":"30 DAYS PD CHEQUE","Credit限制金额":"2,000.00","Comment1":"30D PDC /10/11-NSF=WR BK ACCT","Comment2":"","持有":"1","价目表":"EASTGEN" }]Bill to Phone":"416-555-5555","Tax Schedule ID":"HST ON/S","Location Code":"TOR","Payment Terms ID":"30 DAYS PD CHEQUE","Credit限制金额":"2,000.00","Comment1":"30D PDC /10/11-NSF=WR BK ACCT","Comment2":"","持有":"1","价目表":"EASTGEN" }]

.

然后我使用一些 str_replace 来组织输出并使其看起来不错。这就是我所拥有的:

$newArray = str_replace(":",":",json_encode($newArray));

$newArray = str_replace("[","[\n",$newArray);

$newArray = str_replace("{","{\n",$newArray);

$newArray = str_replace(",",",\n",$newArray);

$newArray = str_replace("},","\n},",$newArray);

$newArray = nl2br($newArray);

.

我遇到的问题是输出如下所示:

[

{

"客户编号": "12345",

"客户名称": "客户 1",

"地址代码": "PRIMARY",

"销售人员 ID": "aa",

“运送到地址 1”:“12345”,

“运送到地址 2”:“123,

假街”,

"运送到地址 3": "",

“运往城市”:“多伦多”,

“运送到省”:“安大略省”,

“运送到邮政编码”:“A1A 1A1”,

“国家代码”:“CA”,

“运送到国家”:“加拿大”,

“运送到联系人”:“John Doe”,

“运送到电话”:“416-555-5555”,

“发送至传真”:“416-555-5552”,

“运输方式”:“我们的卡车”,

“账单地址 1”:“12345”,

“账单地址 2”:“123,

假街”,

"账单地址 3": "",

“比尔到城市”:“多伦多”,

“省法案”:“安大略省”,

“账单到邮政编码”:“A1A 1A1”,

“账单到电话”:“416-555-5555”,

"税表 ID": "HST ON/S",

“位置代码”:“TOR”,

“付款条款 ID”:“30 DAYS PD CHEQUE”,

“信用额度”:“2,

000.00",

“评论 1”:“30D PDC /10/11-NSF=WR BK ACCT”,

“评论2”:“”,

“保持”:“1”,

“价目表”:“EASTGEN”}]

请注意发货地址 2、账单地址 2 和信用额度金额。因为我的 str_replace 它在那里添加了一个换行符。问题是我使用的读取 json 的应用程序需要它在同一行上才能正确注册。有没有办法可以用换行符替换逗号,除非它们是地址或其他变量的一部分?

4

1 回答 1

1

为什么要修改 JSON 编码的字符串?如果您只是想让它看起来“漂亮”,那么您应该使用json_encode($str, JSON_PRETTY_PRINT)- 不要对字符串进行任何其他修改。

于 2013-06-27T16:49:02.523 回答