我正在尝试解组此文件:
{
"@babel/code-frame@7.0.0": {
"licenses": "MIT",
"repository": "https://github.com/babel/babel/tree/master/packages/babel-code-frame",
"publisher": "Sebastian McKenzie",
"email": "sebmck@gmail.com",
"path": "/Users/lislab/workspace/falcon-enrolment/frontend-customer/node_modules/@babel/code-frame",
"licenseFile": "/Users/lislab/workspace/falcon-enrolment/frontend-customer/node_modules/@babel/code-frame/LICENSE"
},
"json-schema@0.2.3": {
"licenses": [
"AFLv2.1",
"BSD"
],
"repository": "https://github.com/kriszyp/json-schema",
"publisher": "Kris Zyp",
"path": "/Users/lislab/workspace/falcon-enrolment/frontend-customer/node_modules/json-schema",
"licenseFile": "/Users/lislab/workspace/falcon-enrolment/frontend-customer/node_modules/json-schema/README.md"
}
}
进入这个结构:
type Dependency struct {
Name string
URL string
Version string
License string
}
使用这些说明:
dependencies := map[string]*json.RawMessage{}
err = json.Unmarshal(file, &dependencies)
// boilerplate
for key, value := range dependencies {
depVal := map[string]string{}
err = json.Unmarshal(*value, &depVal)
// boilerplate
result = append(result, depVal)
}
这个问题是在“json-schema@0.2.3”中我们有一组许可证而不是一个字符串,因此我显然得到了
json: cannot unmarshal array into Go value of type string
有没有办法自动处理license可以是数组或字符串的字段?
慕田峪7331174
相关分类