普通网友 2015-10-07 19:43
浏览 264

在PHP中使用行分隔的JSON字符串

I am using an API which requires I send it data as new line delimited JSON, for example;

{"id":"67523", "name":"Jason"}
{"id":"69928", "name":"Doug"}

The API accepts single JSON objects, but can accept multiple new line delimited JSON objects in a single batch (more efficient). How do you construct string objects like the above in PHP?

If taking a string like {"id":"67523", "name":"Jason"}, I can json_decode() it to turn it into an associative array (this is accepted by the API endpoint). However, the only way I can see to pass a number of these objects together is within an array, but the API does not accept an array of JSON objects.

  • 写回答

2条回答 默认 最新

  • dongzai0020 2015-10-07 20:16
    关注

    JSON does not allow real line-breaks.
    As you said API is accepting Single Objects and not accepting an array of JSON objects, the below Code will help you somewhat where each object is given index

    {
      0: {
        "id": "67523",
        "name": "Jason"
      },
      1: {
        "id": "69928",
        "name": "Doug"
      }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思