开发者

Incremental JSON parsing in php

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 开发者_StackOverflow中文版json_decode just returns NULL and there doesn't seem to be a way to get the position of the error.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜