Php / as3 regex to split multiple json in one

For example, I have this line of two json objects:

{"glossary": {"title": "example glossary"}, "aaa": "1212"} {"adada": "faka"}

I want to split it in an array for PHP and ActionScript 3

Array ( [0] => '{"glossary": {"title": "example glossary"}', [1] => '{"adada": "faka"}' ) 

What is the best way to do this.

Edit:

I do not need to answer how to parse json. To simplify, I need to split

{... {..} ....} {....} {........}

in

{... {..} ....}

{....}

{........}

+1
source share
4 answers

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

+2
source

Regex cannot handle this. If you really don't need a JSON parser, you need to write a simple parsing function.

Something like that:

 function splitJSONString($json) { $objects = array(); $depth = 0; $current = ''; for ($i = 0; $char = substr($json, $i, 1); $i++) { $current .= $char; if ($char == '{') { $depth += 1; } else if ($char == '}' && $depth > 0) { $depth -= 1; if ($depth == 0) { array_push($objects, $current); $current = ''; } } } return $objects; } 
+1
source

Sorry to post on an old question, but I just ran into this question when searching for a similar problem, and I think I have a pretty easy solution for the specific problem that was identified in the question.

 // Convert original data to valid JSON array $json = '['.str_replace('}{', '},{', $originalData).']'; $array = json_decode($json, true); $arrayOfJsonStrings = array(); foreach ($array as $data) { $arrayOfJsonStrings[] = json_encode($data); } var_dump($arrayOfJsonStrings); 

I think this should work as long as your JSON data does not contain strings that may include '} {'.

+1
source

Regex is not suitable for this - use JSON Parser

0
source

All Articles