我有从json响应获取信息的问题,我试图仅从json响应的详细对象获取标签和值的信息,但是我无法获取这些信息,因为在将{json转换为来自{{的c#类时3}}为'General','AC Adapter'等提供了不同的类。但是在我的情况下它不起作用,因为响应具有动态类,因此我无法创建类。
{
"data": {
"General": {
"label": "General",
"details": [
{
"label": "Operating System",
"value": "Google Chrome OS"
},
{
"label": "Product Type",
"value": "Chromebook"
}
]
},
"AC Adapter": {
"label": "AC Adapter",
"details": [
{
"label": "Input",
"value": "AC 120/230 V ( 50/60 Hz )"
},
{
"label": "Output",
"value": "45 Watt , 20 V , 2.25 A"
}
]
},
"Audio & Video": {
"label": "Audio & Video",
"details": [
{
"label": "Camera",
"value": "Yes - 720p"
},
{
"label": "Graphics Processor",
"value": "Intel HD Graphics"
},
{
"label": "Resolution",
"value": "1 Megapixel"
},
{
"label": "Sound",
"value": "Stereo speakers , microphone"
}
]
},
"Battery": {
"label": "Battery",
"details": [
{
"label": "Capacity",
"value": "45 Wh"
},
{
"label": "Run Time",
"value": "Up to 10 hours"
},
{
"label": "Technology",
"value": "3-cell lithium ion"
}
]
},
"Communications": {
"label": "Communications",
"details": [
{
"label": "Features",
"value": "Dual stream (2x2)"
},
{
"label": "Wireless",
"value": "Bluetooth 4.0, 802.11a/b/g/n/ac"
},
{
"label": "Wireless Controller",
"value": "Intel Dual Band Wireless-AC 7260 - M.2 Card"
}
]
},
"Connections & Expansion": {
"label": "Connections & Expansion",
"details": [
{
"label": "Interfaces",
"value": "USB 3.0 � 2 x USB 2.0 � HDMI � Headphone/microphone combo jack"
},
{
"label": "Memory Card Reader",
"value": "Yes ( microSD )"
}
]
},
"Dimensions & Weight": {
"label": "Dimensions & Weight",
"details": [
{
"label": "Dimensions (WxDxH)",
"value": "11.8 in x 8.5 in x 0.9 in"
},
{
"label": "Weight",
"value": "2.84 lbs"
}
]
},
"Display": {
"label": "Display",
"details": [
{
"label": "Features",
"value": "Anti-glare"
},
{
"label": "Image Aspect Ratio",
"value": "16:9"
},
{
"label": "LCD Backlight Technology",
"value": "LED backlight"
},
{
"label": "Resolution",
"value": "1366 x 768 ( HD )"
},
{
"label": "Type",
"value": "11.6\""
},
{
"label": "Widescreen",
"value": "Yes"
}
]
},
"Input": {
"label": "Input",
"details": [
{
"label": "Features",
"value": "Spill-resistant"
},
{
"label": "Type",
"value": "Keyboard, touchpad"
}
]
},
"Manufacturer Warranty": {
"label": "Manufacturer Warranty",
"details": [
{
"label": "Service & Support",
"value": "Limited warranty - 1 year - carry-in"
}
]
},
"Memory": {
"label": "Memory",
"details": [
{
"label": "Max RAM Supported",
"value": "8 GB"
},
{
"label": "RAM",
"value": "4 GB ( provided memory is soldered )"
},
{
"label": "Speed",
"value": "1600 MHz"
},
{
"label": "Technology",
"value": "DDR3L SDRAM"
}
]
},
"Miscellaneous": {
"label": "Miscellaneous",
"details": [
{
"label": "Features",
"value": "Security lock slot (cable lock sold separately), administrator password, hard drive password, power-on password"
},
{
"label": "Included Accessories",
"value": "Power adapter"
},
{
"label": "Localization",
"value": "English"
},
{
"label": "Manufacturer Selling Program",
"value": "TopSeller"
}
]
},
"Processor / Chipset": {
"label": "Processor / Chipset",
"details": [
{
"label": "64-bit Computing",
"value": "Yes"
},
{
"label": "CPU",
"value": "Intel Celeron N3050 / 1.6 GHz"
},
{
"label": "Cache",
"value": "2 MB"
},
{
"label": "Features",
"value": "Integrated memory controller"
},
{
"label": "Max Turbo Speed",
"value": "2.16 GHz"
},
{
"label": "Number of Cores",
"value": "Dual-Core"
}
]
},
"Storage": {
"label": "Storage",
"details": [
{
"label": "Main Storage",
"value": "16 GB SSD - ( eMMC )"
}
]
}
}
}
答案 0 :(得分:1)
http://json2csharp.com/为“常规”,“交流适配器”等提供了不同的类
效果更好:https://quicktype.io/?l=cs&r=json2csharp
但是,我认为你的data
实际上是一本字典。所以最终合同:
public class Response
{
[JsonProperty("data")]
public Data Data { get; set; }
}
public class Data : Dictionary<string, Item> { }
public class Item
{
[JsonProperty("details")]
public Detail[] Details { get; set; }
[JsonProperty("label")]
public string Label { get; set; }
}
public class Detail
{
[JsonProperty("label")]
public string Label { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
}
如果你使用Newtonsoft,反序列化:
var response = JsonConvert.DeserializeObject<Response>(jsonString);
答案 1 :(得分:0)
如果您只想将json字符串转换为C#动态对象,请查看this SO question。代码是最终版本。