Incremental JSON parsing in php

你说的曾经没有我的故事 提交于 2020-01-23 04:27:07

问题


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

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

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.


回答1:


Update

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!

--

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()?

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).

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




回答2:


I've written a SAX-like JSON streaming parser that should do the trick. Let me know if it works!




回答3:


There's a simple work-around, if each individual element is guaranteed to be received in it's entirety, or in other words - you can't get e.g. just the half of an object like this:

{"a": 1,

json_decode() will return NULL because the string you're passing to it is not a valid JSON string. Replace the trailing comma with an ending bracket and there you go:

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

There's no problem in decoding it now and wait for other parts of the stream to be received later.



来源:https://stackoverflow.com/questions/7311402/incremental-json-parsing-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!