我要做的是将一个JSON数组(来自Guzzle)发送到我的SQL数据库。我已经达到了能够获得响应并在网页上显示获得的JSON数组的程度。该数组定义为$data
变量。 $data
变量使用以下方法解码:
$data = json_decode($response->getBody()->getContents());
这可以获得JSON并解码它没有问题。我坚持的部分是获取$data
变量,处理它并将其发送到我的数据库。据我所知,您需要将JSON转换为数组,然后将其发送到数据库。
JSON格式如下:
[{
"INTLDES": "2017-042Z",
"NORAD_CAT_ID": "42848",
"OBJECT_TYPE": "TBA",
"SATNAME": "OBJECT Z",
"COUNTRY": "TBD",
"LAUNCH": "2017-07-14",
"SITE": "TTMTR",
"DECAY": null,
"PERIOD": "96.52",
"INCLINATION": "97.61",
"APOGEE": "597",
"PERIGEE": "586",
"COMMENT": null,
"COMMENTCODE": null,
"RCSVALUE": "0",
"RCS_SIZE": null,
"FILE": "6242",
"LAUNCH_YEAR": "2017",
"LAUNCH_NUM": "42",
"LAUNCH_PIECE": "Z",
"CURRENT": "Y",
"OBJECT_NAME": "OBJECT Z",
"OBJECT_ID": "2017-042Z",
"OBJECT_NUMBER": "42848"
}]
我的卫星模型是这样的:
protected $fillable = [
'intldes',
'norad_cat_id',
'object_type',
'satname',
'country',
'launch',
'site',
'decay',
'period',
'inclination',
'apogee',
'perigee',
'comment',
'commentcode',
'rcsvalue',
'rcs_size',
'file',
'launch_year',
'launch_num',
'launch_piece',
'current',
'object_name',
'object_id',
'object_number'
];
我的迁移文件:
Schema::create('satellites', function (Blueprint $table) {
$table->increments('id');
$table->string('intldes');
$table->string('norad_cat_id');
$table->string('object_type');
$table->string('satname');
$table->string('country');
$table->string('launch')->nullable();
$table->string('site')->nullable();
$table->string('decay')->nullable();
$table->string('period')->nullable();
$table->string('inclination')->nullable();
$table->string('apogee')->nullable();
$table->string('perigee')->nullable();
$table->string('comment')->nullable();
$table->string('commentcode')->nullable();
$table->string('rcsvalue')->nullable();
$table->string('rcs_size')->nullable();
$table->string('file')->nullable();
$table->string('launch_year')->nullable();
$table->string('launch_num')->nullable();
$table->string('launch_piece')->nullable();
$table->string('current')->nullable();
$table->string('object_name');
$table->string('object_id');
$table->string('object_number');
$table->timestamps();
});
我尝试制作一个$object
数组,但这不起作用。
TL; DR:我想采用$data
变量,其中包含已解码的JSON并创建一些内容,以便将其保存到我的卫星SQL数据库中。
编辑:以下是完整的卫星控制器:
public function displayer(){
$api = new Client([
'base_uri' => 'https://www.space-track.org',
'cookies' => true,
]); $api->post('ajaxauth/login', [
'form_params' => [
'identity' => '#',
'password' => '#',
],
]);
$response = $api->get('basicspacedata/query/class/satcat/orderby/INTLDES%20desc/limit/5/metadata/false');
$data = json_decode($response->getBody()->getContents(), true);
$data = array_change_key_case($data, CASE_LOWER);
$model = Satellite::create($data);
dd($data);
}
答案 0 :(得分:2)
看起来你的JSON键名与你的模型属性很好地匹配,除了大写之外。
尝试将数据键映射为小写,然后创建模型实例。
根据@OmisakinOluwatobi建议,您可以使用true
传递给json_decode
以数据形式检索数据。
编辑 - 我错过了您的回复数据是一个对象数组。以下更新将迭代响应数据,并为每个响应数据创建一个新的Satellite
。
// Retrieve data from response
$data = json_decode($response->getBody()->getContents(), true);
// Iterate over response data
foreach ($data as $attributes) {
// Change attribute keys to lowercase
$attributes = array_change_key_case($attributes, CASE_LOWER);
// Create satellite model
Satellite::create($attributes);
}
答案 1 :(得分:0)
实际上就像$encoded = json_encode($request->your_array);
一样简单。这个$encoded
现在将是"可爱的"到SQL数据库。稍后访问编码数据时,可以使用JSON解析器转换回json数组。使用jQuery var your_array = $.parseJSON($response.body);