either you modify the JSON parser to do this, as Amargosh suggested, or you can make a simple algorithm to do this for you, which skips lines and comments and tracks open curly braces. when there are no open braces, then you have full value. repeat it until you enter.
however, my suggestion is to try to solve the problem by talking to the person who is responsible for this conclusion, and convince him to create a valid JSON, which [{"glossary": {"title": "example glossary"}, "aaa": "1212"},{"adada": "faka"}]
edit: to separate JSON objects, you need to prefix each JSON object with a length (4 bytes should be enough). then you read the socket until you get the right amount of characters. the following characters will again be the length of the next object. this is necessary because TCP works in packets. Not only do you have multiple JSON objects in one package, but you can split a single JSON object between two packages.
In addition, a couple of tips:
- Do not use PHP for socket servers. it is not done for this. look haXe , in particular, neko backup.
- do not write this material yourself unless you really need to. it's boring and stupid to work. there are almost millions of socket server solutions. You should also take a look at haXe remoting , which provides a transparent connection between, for example, the flash client and the neko socket server. also please look at smartfox and red5.
edit2: you underestimate the problem and your approach is not very good. you have to build a reliable solution, so that day when you want to send arrays by cable, you will not have a complete failure, because your splitter breaks, or your JSON parser receives incomplete objects, because only half of the object is read, then what you want to do can be easily done: split the input with "}{" , add "}" to any element, but the last and add "{" to any element except the first. Nevertheless, I strongly recommend that you refrain from this approach, because you will regret it at some point. if you really think that you should do it yourself, try at least doing it right.
Greetz
back2dos
back2dos
source share