Accessing objects from a JSON

Asked

Viewed 402 times

2

I have the following file .json, in case I want to access initial data, called suzuki at the beginning of json, how do I get this information?

That one .json is responsible for making the data Seed in the database, and that field is that of the relationship, I just need to pick it up in the class.

The rest of the other data I can take the value and save in the bank.

  • Method of registration to Concessionaire.
    private function fillDealerships(array $files)
    {
        foreach ($files as $file) {
            $data = $this->getFileContent($file);

            foreach ($data as $item) {
                $slug = isset($data['u']) ? Str::slug($item['slug']) : $file['filename'];

                /* @var $dealership Dealership */
                $dealership = Dealership::query()->firstOrCreate([
                    'slug' => $slug,
                    $dealership->brands()
                ], [
                    'name' => $item['n'] ?? $slug,
                ]);

                $this->fillDealershipStore($dealership, $item);
            }
        }
    }
  • Json of the Dealership Stores.
{
  "suzuki": [
    {
      "n": "MOTO MAIS",
      "u": "moto-mais",
      "e": {
        "l": "Av. Oswaldo Da Silva",
        "n": "710",
        "m": "",
        "b": "Conjunto Habitacional Ana Jacinta",
        "u": "19064070",
        "c": {
          "i": "9286",
          "n": "Presidente Prudente"
        },
        "s": {
          "i": "26",
          "n": "Sao Paulo"
        },
        "r": "https:\/\/goo.gl\/maps\/aYoJgneRsTo",
        "g": [
          "-22.1505632",
          "-51.4526682"
        ]
      },
      "c": {
        "t": {
          "c": {
            "n": "(18) 3901-1911",
            "l": "1839011911"
          }
        }
      },
      "r": {},
      "g": null,
      "t": "concessionaria-autorizada"
    }
}
    private function getFileContent(array $file)
    {
        return json_decode(file_get_contents($file['path']), true)[$file['filename']];
    }

1 answer

1


In this specific case just use the key native of php that it will return the first key of your json.

foreach ($files as $file) {
     $data = $this->getFileContent($file);
     $key = key($data); // <-- "suzuki"

Editing

The problem is that when you call the function getFileContent she breaks the json showing the data from the array.

    json_decode(file_get_contents($file['path']), true)[$file['filename']]; // <-- aqui


    {
      "suzuki": 
      [ // <-- aqui
        {
          "n": "MOTO MAIS",
          "u": "moto-mais",
          "e": { ...

Then the Dice becomes inaccessible within the function.

Solution:

  • First change the function getFileContent for:

    return json_decode(file_get_contents($file['path']), true);
    
  • Then change the function fillDealerships thus:

    foreach ($files as $file) {
         $data = $this->getFileContent($file);
         $key = $file['filename']; // <-- "suzuki"
         $data = $data[$key]; // <-- array
    
  • Here he returned 0

  • @Romulosousa understood... your json already enters formatted in function... It starts by array. That is, within this function is impossible. 'Cause the key doesn’t exist inside her.

  • @Romulosousa how is the array for the function launched? Put the code in the question.

  • I added the method to read the file in the question.

  • @Romulosousa already know... I will edit.

  • Because with this data I make the relationship between the dealership and the brand.

  • @Romulosousa edited.

  • @Romulosousa worked out?

  • It was, but there was an error, in my part, I can not access in its way, that’s right, but because of the loop, being that the name of the file is in $file['filename]`. ;array:2 [&#xA; "filename" => "haojue"&#xA; "path" => "C:\wamp64\www\suzuki-cms-backoffice\database\migrations\production\dealerships\haojue.json"&#xA;]

  • @Romulosousa but the $file['filename] is not "Suzuki" ??

  • @Romulosousa tries the way I edited... in the loop.

  • I did so $filename = $file['filename']; He returns to me the same as you :D

  • @Romulosousa returns Suzuki right? I had written wrong! = ) I put an edition.

  • Suzuki would be the value that would be caught, all right.

  • Now I have a problem taking this data that serves to link the relationship between the dealership and brand

Show 11 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.