duandian8251 2011-10-17 11:38
浏览 8

{和[在json中使用json_encode在php中有什么不同

What is different between { and [ in json

model 1:

"cell": { "id": "3961" }

model 2:

"cell": [ "id": "3961" ]

how can i transfer model 1 to 2 via json_encode ?

  • 写回答

4条回答 默认 最新

  • duanliang1898 2011-10-17 11:39
    关注

    { is an Object, [ is an Array.

    See the official Docs.

    In your case, the first example is a normal Object with a property called id. The second example is an associative array with an index called id. JSON does not have associative arrays. The second example is invalid JSON.

    This is, due to the fact that JSON is JavaScript Object Notification and JavaScript does not know associative arrays. Instead, JavaScript allows you to dynamically add new property to an Object and enables you to access any Object-property using the brackets: Object['property'].

    So, Objects are (kind of) associative arrays in JavaScript and therefor in JSON.

    Since the json_encode()-function encodes creates the JSON-String from the supplied object, you'll need to pass an object instead of an array.

    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题