V
V
Vitalik_vlk2014-08-21 11:48:39
PHP
Vitalik_vlk, 2014-08-21 11:48:39

Parsing multi-storey json?

Hello.
There is a huge json:, I try to process it through json_decode and get:

Array ( [0] => Array ( [0] => 86071475 [1] => {"completed_categorized_achievements":{"0":{"0":[1408604553,1408605237],"5":[1408604553],"7":[1408605237],"6":[1408604802],"9":[1408606378],"8":[1408605930]},"1":{"24":[1408605140],"10":[1408605133],"13":[1408607221],"12":[1408605049,1408605049,1408605049],"15":[1408604551,1408605026,1408605569],"14":[1408604553,1408606378],"22":[1408604641,1408605140,1408605682],"16":[1408604519,1408605029],"19":[1408604686,1408605596],"18":[1408604544,1408605617],"1":[1408604721,1408605167],"3":[1408606301],"2":[1408605860],"5":[1408605291],"4":[1408605580,1408607753],"7":[1408605123,1408605167],"6":[1408605159,1408605518,1408605518],"9":[1408604641],"8":[1408605128]},"2":{"12":[1408607112],"7":[1408604721],"6":[1408605010,1408605608,1408606142]}},"division":{"v_div":0,"h_div":0,"need_promoted":false},"static_resources":{"used_talents":0,"army_strength":45,"driving_skill":0,"achievement_points":460},"wish_list":{},"global_collection_wish_item":null,"analytics":{"generation":0,"source":"default"},"talents":{},"expiring_resources":{"collection_receive":null,"global_collection_receive":null},"names":{"squad_name":"\u0411\u0440\u0443\u043a\u043b\u0438\u043d"},"static_maximum_resources":{"srup":{"amount":2,"maximum":2},"srua":{"amount":1,"maximum":1},"srut":{"amount":1,"maximum":1}},"categorized_medal_slots":{"0":{"category":"1","medal":"0","group":"4"},"1":{"category":"0","group":"0","medal":"0"}},"current_territory":"1","medal_slots":{},"chips":{"1":"0","0":"1","2":"0"},"uniform":"0","territories":{"1":{"collected":null,"doll":{"1":"1","2":"0","3":"0","4":"1"}},"0":{"collected":null},"2":{"collected":null}},"version":90,"last_login":1408610248,"phrases":{},"experiences":{"experience":5927},"completed_achievements":{},"decks":{"1":{"parts":{"0":{"cards":{"cards":{"10":{"0":{"grade":"0","flag":null}}}},"current_card":{"index":"0","card":"10"}}}},"0":{"parts":{"1":{"cards":{"cards":{"10":{"0":{"grade":"0","flag":null}}}},"current_card":{"index":"0","card":"10"}},"0":{"cards":{"cards":{"10":{"0":{"grade":"0","flag":null}}}},"current_card":{"index":"0","card":"10"}}}},"2":{"parts":{"0":{"cards":{"cards":{"10":{"0":{"grade":"0","flag":null}}}},"current_card":{"index":"0","card":"10"}}}}}} ) )

Ie, it processes, but not completely
. How can I get access to the remaining piece of json?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Zelenin, 2014-08-21
@Vitalik_vlk

6 is

JSON_ERROR_RECURSION One or more recursive references in the value to be encoded

actually what you have is json in json
php.net/manual/en/function.json-last-error.php

1
1Michael1, 2014-08-21
@1Michael1

most likely the problem is in the correctness of json itself
json_decode skips values ​​if they were formed incorrectly (not according to the described standard)
For debug: after json_decode insert something like:

switch (json_last_error()) {
    case JSON_ERROR_NONE:
        echo 'No errors';
        break;
    case JSON_ERROR_DEPTH:
        echo 'Maximum stack depth exceeded';
        break;
    case JSON_ERROR_STATE_MISMATCH:
        echo 'Underflow or the modes mismatch';
        break;
    case JSON_ERROR_CTRL_CHAR:
        echo 'Unexpected control character found';
        break;
    case JSON_ERROR_SYNTAX:
        echo 'Syntax error, malformed JSON';
        break;
    case JSON_ERROR_UTF8:
        echo 'Malformed UTF-8 characters, possibly incorrectly encoded';
        break;
    default:
        echo 'Unknown error';
        break;
}

Well, then proceed according to the circumstances :)

S
Sergey, 2014-08-21
Protko @Fesor

I still strongly doubt that you have standard JSON. It is ignored, not parsed. So we can assume that inside json is another json as a string and it needs to be parsed separately. For if there was an error in depth, json_decode would return null and then it would be possible to catch it through json_get_error (or something like that). The documentation has an example of how to handle such cases.
Also try third party parsers: https://github.com/Seldaek/jsonlint for example. It should parse normally if you have normal json.
If you have a puff with JSON, then you need to manually recursively bypass the resulting array / object and try to do json_decode. If it fits, leave it. NOT amenable - leave the old value.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question