1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved JSON file constantly breaks

Discussion in 'Development' started by Mohagames205, May 19, 2019.

  1. Mohagames205

    Mohagames205 Spider Jockey

    Messages:
    26
    GitHub:
    mohagames205
    Hi everyone

    I made a plugin that can lock/unlock doors, I save all the doors in a JSON file. This is what the JSON file normally looks like:
    Code:
    {
        
    [
    {
    "key_name": "Gemeentehuis",
    "coords": {
    "x": 265,
    "y": 13,
    "z": 286
    }
    },
    {
    "key_name": "Gemeentehuis",
    "coords": {
    "x": 261,
    "y": 13,
    "z": 296
    }
    },
    {
    "key_name": "Gemeentehuis",
    "coords": {
    "x": 262,
    "y": 12,
    "z": 294
    }
    },
    {
    "key_name": "Gemeentehuis",
    "coords": {
    "x": 261,
    "y": 13,
    "z": 295
    }
    }
    ]
    }

    But whenever I remove something from the JSON file, with this code:
    The code basically checks if the touched block is the same as the block in the JSON file, if it is the same it will remove it and update the JSON file.

    PHP:
    if(isset($this->unlockSession[$player->getName()])){
                if(
    $this->unlockSession[$player->getName()]){
                    
    $event->setCancelled();
                    
    $x $event->getBlock()->getX();
                    
    $y $event->getBlock()->getY();
                    
    $z $event->getBlock()->getZ();
                    
    $json_file = (array) json_decode(file_get_contents($this->pathtrue));
                    
    $index 0;
                    foreach (
    $json_file as $value) {
                        
    $x_j $value->coords->x;
                        
    $y_j $value->coords->y;
                        
    $z_j $value->coords->z;
                        
    var_dump(array($x_j$y_j$z_j));

                        if (
    $x == $x_j && $z == $z_j) {
                            if(
    abs($y $y_j) <= || abs($y_j $y) <= 1) {
                                break;
                            }
                        }
                        
    $index += 1;
                    }
                    
    var_dump($json_file);
                    echo 
    "------------------------------------\n";
                    
    var_dump($json_file[$index]);
                    echo 
    "------------------------------------\n";
                    unset(
    $json_file[$index]);
                    
    var_dump($json_file);
                    
    $new_json json_encode($json_fileJSON_PRETTY_PRINT);
                    
    file_put_contents($this->path$new_json);
                    
    $player->sendMessage("§aDe vergrendeling is opgeheven!");
                    unset(
    $this->unlockSession[$player->getName()]);
                }
            }
        }

    Now after I run this code, my JSON file turns like this:
    Code:
    {
        "1": {
            "key_name": "Gemeentehuis",
            "coords": {
                "x": 261,
                "y": 13,
                "z": 296
            }
        },
        "2": {
            "key_name": "Gemeentehuis",
            "coords": {
                "x": 262,
                "y": 12,
                "z": 294
            }
        },
        "3": {
            "key_name": "Gemeentehuis",
            "coords": {
                "x": 261,
                "y": 13,
                "z": 295
            }
        }
    }

    Suddenly there appears "1", "2" and "3". I don't know from where this comes or why it is there.
    Instead I expect this to happen:
    Code:
    [
        {
            "key_name": "Gemeentehuis",
            "coords": {
                "x": 261,
                "y": 13,
                "z": 296
            }
        },
        {
            "key_name": "Gemeentehuis",
            "coords": {
                "x": 262,
                "y": 12,
                "z": 294
            }
        },
        {
            "key_name": "Gemeentehuis",
            "coords": {
                "x": 261,
                "y": 13,
                "z": 295
            }
        }
    ]

    You can see my whole code here: https://hastebin.com/azoharuhok.xml

    I hope someone can help!
     
  2. mm899

    mm899 Spider Jockey

    Messages:
    32
    maybe try using associative arrays and serialising it when saving to the file and when reading it just unserialise it
     
  3. mm899

    mm899 Spider Jockey

    Messages:
    32
    so something like:
    PHP:
    $keys = [];


    $temp = array(
        
    'KeyName' => 'exampleKeyName',
        
    'x' => 1,
        
    'y' => 2,
        
    'z' => 3
    );

    array_push($keys$temp);

    var_dump($keys);

    //echo $temp['KeyName'];
    Every time a new lock is made push an associative array into $keys
     
  4. mm899

    mm899 Spider Jockey

    Messages:
    32
    • Excessive Posting
    When loading or saving the data
    PHP:
    serialize($keys);

    unserialize($keys);
     
  5. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    The problem is that you are using unset, which will remove a value from the array, but not reorder the indices.
    See this example:
    PHP:
    <?php

    $arr 
    = ["hello""this""isatest"];
    var_dump($arr);
    unset(
    $arr[0]);
    var_dump($arr);
    will output
    Code:
    array(3) {
      [0]=>
      string(5) "hello"
      [1]=>
      string(4) "this"
      [2]=>
      string(7) "isatest"
    }
    array(2) {
      [1]=>
      string(4) "this"
      [2]=>
      string(7) "isatest"
    }
    
    As you can see, the array now starts at one.
    If you will then json encode it, in order to preserve that order, the json encoder will put those indices there.
    You could use array_values() before serialising it to json, or use array_splice() instead of unset.
    See this Stackoverflow post for more information.
     
    Mohagames205, mm899 and Sandertv like this.
  6. Mohagames205

    Mohagames205 Spider Jockey

    Messages:
    26
    GitHub:
    mohagames205
    Hi, thank you very much! I now understand why it didn't work and I fixed the problem. Have a nice day :)
     
    HimbeersaftLP and mm899 like this.
  7. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    Just a moderator note: next time please edit the message if you have more information to add, rather than sending a new message
     
    SOFe and HBIDamian like this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.