PHP中的增量JSON解析 [英] Incremental JSON parsing in php

查看:159
本文介绍了PHP中的增量JSON解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在php程序中,我想逐步解析JSON.例如,给定部分JSON

In a php program, I want to parse JSON incrementally. For example, given the partial JSON

[1, 2, {"id": 3},

我想在流传输其余JSON输入之前获得1、2和字典. php的 json_decode 只会返回NULL,而且似乎没有一种获取位置的方法.错误.

I want to get 1, 2 and the dictionary even before the rest of the JSON input is streamed. php's json_decode just returns NULL and there doesn't seem to be a way to get the position of the error.

推荐答案

更新

我编写了一个小类,可以逐字符进行JSON输入解析. https://github.com/janeklb/JSONCharInputReader

I've written a small class that does char-by-char JSON input parsing.. https://github.com/janeklb/JSONCharInputReader

关闭印刷机,所以可能会有一些错误..如果您决定尝试一下,请告诉我!

Fresh off the presses so it's probably got a few bugs.. if you decide to try it out, let me know!

-

您(在跟踪'{','[',']','}'范围的同时,可以在每个不属于字符串值的逗号处中断流吗? 然后使用json_decode()处理每个令牌?

Could you (while keeping track of '{', '[', ']', '}' scope) break the stream up on each comma that's not part of a string value? And then process each token using json_decode()?

如果json流中没有很多大对象(因为它们只有在完全到达后才会被解析),所以该解决方案最有效.

This solution would work best if the json stream didn't have a lot of large objects (as they would only be parsed once they've arrived in full).

如果确实有大对象,则可以对该策略进行修改以使其看起来更深"..但是这里的复杂性会上升.

if it does have large objects, this strategy could be modified to look a little 'deeper' instead.. but the complexity here would shoot up.

这篇关于PHP中的增量JSON解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆