duanhangjian8149
2012-10-25 18:43 阅读 34

通过PHP重复密钥的JSON数据

Apache SOLR calls for a quirky JSON format when you want to add multiple entries at once. ( http://wiki.apache.org/solr/UpdateJSON#Update_Commands -- notice the 'add' nodes)

In a nutshell, I need to be able to create JSON that looks like this:

{
   "key": "val 1",
   "key": "val 2"
}

In PHP, you can easily create an array, but this JSON structure calls for an object with two keys of identical name that are explicitly not in an array.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dongtuoleng8624 dongtuoleng8624 2012-10-25 18:57

    You aren't going to be able to do this using json_encode because it's not valid JSON. (Keyspace collision)

    You are going to need to assemble the object manually.

    You might consider creating the individual items, then using implode(). Then you can prepend and append { and }.

    This would actually feel cleaner to me then hacking away at PHP to get json encoding to work.

    Suggested Alternative

    I'm a big fan of the Solarium Project for PHP and Solr.

    Bulk Adds and pretty easy take a look here: http://wiki.solarium-project.org/index.php/V2:BufferedAdd_plugin

    点赞 评论 复制链接分享

相关推荐