如何在数据库中插入所有json文件数据?

时间:2019-06-14 10:06:55

标签: laravel

我有JSON文件,并且其中有数据。我想通过DB播种器导入数据库中的所有数据。我收到一个错误Trying to get property name of non-object。我有多个数据,该如何插入数据库?

public function run()
{
    $json = File::get("public/kmz/WASASubdivisions.geojson");
    $data = json_decode($json);
 //        dd($data);
    foreach ($data as $obj){
        Regions::create(array(
            'name' => $obj[0]->Name,
            'description' => $obj[0]->description,
            'altitudeMode' => $obj[0]->altitudeMode,
            'Town' => $obj[0]->Town,
            'AC' => $obj[0]->AC,
            'No_of_TW' => $obj[0]->No_of_TW,
            'No' => $obj[0]->No,
            'DC'=> $obj[0]->DC,
            'HH_2017' => $obj[0]->HH_2017,
            'FID' => $obj[0]->FID,
            'Area_ha' => $obj[0]->Area_ha,
            'Field_1' => $obj[0]->Field_1,
            'Pop_Dens' => $obj[0]->Pop_Dens,
            'Id' => $obj[0]->Id,
            'Pop_2017' => $obj[0]->Pop_2017,
            'Area_Sq'=> $obj[0]->Area_Sq,
        ));
    }
}

示例Json格式

31 => {#837
  +"type": "Feature"
  +"properties": {#838
    +"Name": "Gujjar Pura"
    +"description": null
    +"altitudeMode": "clampToGround"
    +"Town": "Shalimar Town"
    +"AC": "31"
    +"No_of_TW": "11"
    +"No": "13"
    +"DC": "38"
    +"HH_2017": "30478"
    +"FID": "31"
    +"Area_ha": "648.327"
    +"Field_1": "Gujjar Pura"
    +"Pop_Dens": "54063.141167"
    +"Id": "0"
    +"Pop_2017": "196619"
    +"Area_Sq": "3.63684"
  }
  +"geometry": {#839
    +"type": "MultiPolygon"
    +"coordinates": array:1 [
      0 => array:1 [
        0 => array:169 [ …169]
      ]
    ]
  }
}

4 个答案:

答案 0 :(得分:1)

public function run()
{
    $json = File::get("public/kmz/WASASubdivisions.geojson");
    $data = json_decode($json);
    dd($data);
    foreach ($data as $obj){
        Regions::create(array(
            'name' => $obj->Name,
            'description' => $obj->description,
            'altitudeMode' => $obj->altitudeMode,
            'Town' => $obj->Town,
            'AC' => $obj->AC,
            'No_of_TW' => $obj->No_of_TW,
            'No' => $obj->No,
            'DC'=> $obj->DC,
            'HH_2017' => $obj->HH_2017,
            'FID' => $obj->FID,
            'Area_ha' => $obj->Area_ha,
            'Field_1' => $obj->Field_1,
            'Pop_Dens' => $obj->Pop_Dens,
            'Id' => $obj->Id,
            'Pop_2017' => $obj->Pop_2017,
            'Area_Sq'=> $obj->Area_Sq,
        ));
    }
}

答案 1 :(得分:0)

因为我不太喜欢将json作为对象处理

因此json_decode将接受第二个参数,因此

$json = File::get("public/kmz/WASASubdivisions.geojson");
$data = json_decode($json,true);

dd($data);

foreach ($data as $obj)
    {
    Regions::create(array(
            'name' => $obj['Name'],
    'description' => $obj['description'],
    'altitudeMode' => $obj['altitudeMode'],
    'Town' => $obj['Town'],
    'AC' => $obj['AC'],
    'No_of_TW' => $obj['No_of_TW'],
    'No' => $obj['No'],
    'DC'=> $obj['DC'],
    'HH_2017' => $obj['HH_2017'],
    'FID' => $obj['FID'],
    'Area_ha' => $obj['Area_ha'],
    'Field_1' => $obj['Field_1'],
    'Pop_Dens' => $obj['Pop_Dens'],
    'Id' => $obj['Id'],
    'Pop_2017' => $obj['Pop_2017'],
    'Area_Sq'=> $obj['Area_Sq'],
        ));
    }

您可以发布表的 dd()结果和字段集

答案 2 :(得分:0)

让我们支持吧,我有一个模型Post,我想以JSON格式将其他数据保存在posts表中。在这种情况下,我们可以在Laravel中使用属性$casts。它将把我们的领域价值转化为我们所付出的一切。

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $table='posts';

    protected $fillable = ['user_id', 'title', 'short_description', 'description', 'status', 'json_data'];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'json_data' => 'array',
    ];
}

现在我们要保存类似这样的数据

$data = [
    'user_id' => 1,
    'title' => 'abc',
    'short_description' => 'test',
    'description' => 'test',
    'status' => true,
    'json_data' => [
        'additional_info' => '',
        'post_image' => '',
        ...
    ],
];

$item = new Post;
$item->fill($data)->save();

这会将您的json_data数组值保存到数据库中的JSON中。但是,当您从数据库中获取数据时,它将自动将其转换为数组。

供参考,请阅读this

答案 3 :(得分:0)

这些属性在properties键下,但是您是从对象的根引用它们的。例如$obj[0]->Name应该是$obj[0]->properties->Name,依此类推。