我正在尝试使用 c# 和 json.net反序列化来自 OctoPart API ( http://octopart.com/api/docs/v3/rest-api ) 的结果
反序列化大部分数据没有问题,但是我遇到了 Part.specs 属性(http://octopart.com/api/docs/v3/rest-api#notes-part.specs)的问题,因为这些属性根据返回的项目更改。
以下是 API 关于 Part.Specs 的说明
附加到 Part 实例的 specs 属性是一个 JSON 对象映射属性短名称(例如“voltage_rating_dc”)到QualitativeValue和QuantitativeValue实例。
请务必注意, (Qual|Quant)itativeValue 对象的所有值属性都是 JSON 数组。这样做的原因是为了适应多值属性,例如具有多个输出电压的电源:
因为我想维护类,我相信我可能需要实现一个自定义的 JConverter?我在看这个问题,但不太确定如何应用于此示例,因为属性可以反序列化为 QualitativeValue / QuantitativeValue 类
这是我的部分课程
public class Part
{
public string __class__ { get; set; }
public string uid { get; set; }
public long uid_v2 { get; set; }
public string mpn { get; set; }
public Manufacturer manufacturer { get; set; }
public Brand brand { get; set; }
public string octopart_url { get; set; }
public List<PartOffer> offers { get; set; }
public List<Datasheet> datasheets { get; set; }
public List<ComplianceDocument> compliance_documents { get; set; }
public List<Description> descriptions { get; set; }
public List<ImageSet> imagesets { get; set; }
public Dictionary<string, string> specs { get; set; }
public List<string> category_uids { get; set; }
public List<ExternalLinks> external_links { get; set; }
}
这是来自 API 的示例结果 (PartsMatchResponse)
{
"__class__": "PartsMatchResponse",
"msec": 183,
"request": {
"__class__": "PartsMatchRequest",
"exact_only": false,
"queries": [
{
"__class__": "PartsMatchQuery",
"brand": null,
"limit": 10,
"mpn": "ERJ8BWFR010V",
"mpn_or_sku": null,
"q": "",
"reference": null,
"seller": null,
"sku": null,
"start": 0
}
]
},
"results": [
{
"__class__": "PartsMatchResult",
"error": null,
"hits": 1,
"items": [
{
"__class__": "Part",
"brand": {
"__class__": "Brand",
"name": "Panasonic - ECG",
"uid": "4c528d5878c09b95"
},
"category_uids": [
"7542b8484461ae85",
"cd01000bfc2916c6",
"5c6a91606d4187ad"
],
"compliance_documents": [],
"datasheets": null,
"external_links": {
"__class__": "ExternalLinks",
"evalkit_url": null,
"freesample_url": null,
"product_url": null
},
"imagesets": null,
"manufacturer": {
"__class__": "Manufacturer",
"name": "Panasonic - ECG",
"uid": "c20a0700af7c11cd"
},
"mpn": "ERJ8BWFR010V",
"octopart_url": "http://octopart.com/erj8bwfr010v-panasonic+-+ecg-7979066",
"offers": null,
"specs": {
"case_package": {
"__class__": "QualitativeValue",
"attribution": {
"__class__": "Attribution",
"first_acquired": null,
"sources": []
},
"value": [
"1206"
]
},
"case_package_si": {
"__class__": "QualitativeValue",
"attribution": {
"__class__": "Attribution",
"first_acquired": null,
"sources": []
},
"value": [
"3216"
]
},
"lead_free_status": {
"__class__": "QualitativeValue",
"attribution": {
"__class__": "Attribution",
"first_acquired": null,
"sources": [
{
"__class__": "Source",
"name": "Future Electronics",
"uid": "e4032109c4f337c4"
}
]
},
"value": [
"Lead Free"
]
},
"lifecycle_status": {
"__class__": "QualitativeValue",
"attribution": {
"__class__": "Attribution",
"first_acquired": null,
"sources": []
},
"value": [
"Not Listed by Manufacturer"
]
},
"pin_count": {
"__class__": "QuantitativeValue",
"attribution": {
"__class__": "Attribution",
"first_acquired": null,
"sources": [
{
"__class__": "Source",
"name": "Farnell",
"uid": "58989d9272cd8b5f"
}
]
},
"max_value": null,
"min_value": null,
"unit": null,
"value": [
"2"
]
},
"power_rating": {
"__class__": "QuantitativeValue",
"attribution": {
"__class__": "Attribution",
"first_acquired": null,
"sources": [
{
"__class__": "Source",
"name": "Newark",
"uid": "d294179ef2900153"
}
]
},
"max_value": null,
"min_value": null,
"unit": null,
"value": [
"0.5"
]
},
"resistance": {
"__class__": "QuantitativeValue",
"attribution": {
"__class__": "Attribution",
"first_acquired": null,
"sources": [
{
"__class__": "Source",
"name": "Farnell",
"uid": "58989d9272cd8b5f"
}
]
},
"max_value": null,
"min_value": null,
"unit": null,
"value": [
"0.01"
]
},
"resistance_tolerance": {
"__class__": "QualitativeValue",
"attribution": {
"__class__": "Attribution",
"first_acquired": null,
"sources": []
},
"value": [
"\u00b11%"
]
},
"rohs_status": {
"__class__": "QualitativeValue",
"attribution": {
"__class__": "Attribution",
"first_acquired": null,
"sources": [
{
"__class__": "Source",
"name": "Newark",
"uid": "d294179ef2900153"
}
]
},
"value": [
"Compliant"
]
}
},
"uid": "69e8a09b8cb4b62f",
"uid_v2": 797906654705
}
],
"reference": null
}
]
}