Answer the question
In order to leave comments, you need to log in
How to parse json following each other with nesting?
They gave a stupid api method - it returns json objects one after another without a separator. I'm trying to figure out the most efficient way to sort it all out.
{"param1": "value1","param1": "value1","param1": "value1","param1": {},"param1": "value1"}{"param1": "value1","param1": "value1","param1": "value1","param1": {},"param1": "value1"}{"param1": "value1","param1": "value1","param1": "value1","param1": {},"param1": "value1"}
{"key":"value"}and in such an object may occur
"key":{"param2":"value2"}
(?<=\{\{).*(?=\}\})breaks down
{([^}]*)}breaks ,
Answer the question
In order to leave comments, you need to log in
$str = '{"param1": "value1","param1": "value1","param1": "value1","param1": {},"param1": "value1"}{"param1": "value1","param1": "value1","param1": "value1","param1": {},"param1": "value1"}{"param1": "value1","param1": "value1","param1": "value1","param1": {},"param1": "value1"}';
$arr = explode('}{', $str);
print_r($arr);
Array
(
[0] => {"param1": "value1","param1": "value1","param1": "value1","param1": {},"param1": "value1"
[1] => "param1": "value1","param1": "value1","param1": "value1","param1": {},"param1": "value1"
[2] => "param1": "value1","param1": "value1","param1": "value1","param1": {},"param1": "value1"}
)
You can use json_decode()
to convert to an array, rebuild and return to json back using json_encode()
Your example does not work, because you copied three arrays into one line. The one that works will return you one key => value
$json = '{"param1": "value1","param1": "value1","param1": "value1","param1": {},"param1": "value1"}';
$json = json_decode( $json, true );
var_dump($json); // печатает array(1) { ["param1"]=> string(6) "value1" }
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question