我该如何修复我的json输出?

时间:2017-12-07 04:00:24

标签: json curl

我有一个问题要解码我从CURL收到的json, 我使用json_last_error来查看可能是什么原因,看起来我的json格式不正确。

{
    "RESPONSE_DATA": [{
                "property_address": "6\/192 Kingsgrove Road",
                "price": 0.0,
                "contact_name": "Nicholas Smith",
                "property_facing_direction": "unknown",
                "agent_name": "",
                "client_id": 46984,
                "property_suburb": "Kingsgrove",
                "agent_phone": "",
                "contact_phone": "0407 787 288",
                "ordered_datetime": "2017-12-05 04:15:03",
                "agent_email": "",
                "property_state": "NSW",
                "job_id": 2324,
                "im_job_id": "40432-o",
                "product_ids": 3000000,
                "confirmed_datetime": "",
                "photographer_comment": "Photography Premium Daylight 3 photos  $145.00\
                nAdd Per Premium Photo 2 at $20 .00 each\ n Total $185 .00 ","
                contact_company ":"
                Raine & Horne Commerical - Marrickville ","
                agent_id ":"
                ","
                preferred_datetime ":"
                2017 - 12 - 07 11: 00: 00 ","
                property_postcode ":0000,"
                status_code ":"
                N "}],"
                RESPONSE_MESSAGE ":"
                OK ","
                RESPONSE_CODE ":200}

Click here to see the json output and the issue

来自jsonlint的

,响应就像......

In [1]: MyString = '--"I like salami. "-I want to walk to the shops"'
In [2]: MyString.replace('-', '').replace('"', '')
Out[2]: 'I like salami. I want to walk to the shops'

1 个答案:

答案 0 :(得分:0)

经纪json有4个问题:

  1. 意外的换行
  2. \n
  3. photographer_comment\ n
  4. photographer_comment
  5. property_postcode ":0000
  6. 您可以通过以下方式进行硬编码替换:

    $json = trim(preg_replace('/\s+/', ' ', $json));
    $json = str_replace("\\n", "", $json);
    $json = str_replace("\\ n", "", $json);
    $json = str_replace(":0000", ":\"0000\"", $json);
    

    它快速而肮脏,而不是覆盖其他情况;如果你想使用更通用的方法,你可以试试正则表达式。但我认为在数据提供商方面修复它会更合理。

    除此之外,由于尾部空白空间,某些键的名称不佳,例如:"preferred_datetime "