尝试从yaml加载数据并创建以下对象:
问题:无法将yaml映射到Object,当我尝试将其始终抛出异常时:java.util.LinkedHashMap无法转换为com.heraizen.DataConfig $ Data
Map<String,List<Data>> map;
代码段:
public class DataConfig {
private Map<String, List<Data>> heros = new HashMap<String, List<Data>>();
public static class Data {
private String name;
private String uri;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
@Override
public String toString() {
return "Data [name=" + name + ", uri=" + uri + "]";
}
}
public Map<String, List<Data>> getHeros() {
return heros;
}
public void setHeros(Map<String, List<Data>> heros) {
this.heros = heros;
}
Yaml:
---
heros:
one:
- name: h1
url: hu2
- name: h2
url: hu22
two:
- name: h3
url: hu3
构造对象:
Yaml yaml = new Yaml(new Constructor(DataConfig.class));
DataConfig retValue = (DataConfig) yaml.load(new FileInputStream("one.yaml"));
retValue.getHeros().entrySet().forEach(ele->{
System.out.println(ele.getKey()+" "+ele.getValue().get(0).getName());
});
System.out.println(retValue);
答案 0 :(得分:0)
由于//changelogs.blade.php
var table = $('.data-table').DataTable({
processing: true,
serverSide: true,
ajax: "{{ route('change_logs.index') }}",
columns: [
//{data: 'DT_RowIndex', name: 'DT_RowIndex'},
{data: 'id', name: 'change_logs.id'},
{data: 'user.name', name: 'user.name'},
{data: 'user.username', name: 'user.username'},
{data: 'sector.location.location_name', name: 'sector.location.location_name'},
{data: 'sector.sector_name', name: 'sector.sector_name'},
{data: 'layer_category.layer.layer_name', name: 'layer_category.layer.layer_name'},
{data: 'layer_category.category_name', name: 'layer_category.category_name'},
{data: 'object_name', name: 'object_name'},
{data: 'operation.operation_name', name: 'operation.operation_name'},
{data: 'action', name: 'action', orderable: false, searchable: false},
]
});
//controller
$data = ChangeLog::with('user','sector','operation','layer_category' )->select('change_logs.*');
return Datatables::of($data)
//class ChangeLog
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class ChangeLog extends Model
{
protected $fillable = [
'username', 'sector_id','operation_id', 'layer_category_id', 'object_name'
];
protected $hidden = [
];
public function location()
{
return $this->belongsTo('App\Location');
}
public function user()
{
//return $this->belongsTo('App\User');
return $this->belongsTo('App\User', 'username', 'username');
}
public function sector()
{
return $this->belongsTo('App\Sector')->with('location');
}
public function operation()
{
return $this->belongsTo('App\Operation');
}
public function layer_category()
{
return $this->belongsTo('App\LayerCategory')->with('layer');
}
}
//class Sector
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Sector extends Model
{
protected $fillable = [
'sector_name','location_id',
];
protected $hidden = [
];
public function location()
{
return $this->belongsTo('App\Location');
}
}
//class Location
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Location extends Model
{
protected $fillable = [
'location_name',
];
protected $hidden = [
];
public function sectors()
{
return $this->hasMany('App\Sector');
}
}
不是DataConfig
的子类型或超类型,因此无法在其中使用强制类型转换。我对yaml不熟悉,但是我可以假设方法LinkedHashMap
从您的异常描述中返回了yaml.load()
。
尝试编写属于LinkedHashMap
的静态方法,该方法将DataConfig
转换为LinkedHashMap
:
DataConfig