第三方供应商拥有我正在使用的一系列端点,它们具有以下结构:
[
{
"Id" : 117,
"Name" : "Example"
},
{
"Id" : 118,
"Name" : "Sample"
},
]
我创建了一个对象结构:
public class RootObject<TType>
{
public TType[] ApiModels { get; set; }
}
public class Sample
{
public int Id { get; set; }
public string Name { get; set; }
}
但是当我去反序列化时,我会这样做:
JsonConvert.DeserializeObject<RootObject<Sample[]>>(...);
我收到错误,无法反序列化当前JSON数组?为什么?如果我做动态运动,我可以遍历一切,那我就认为我做的事很愚蠢。
更新错误:
发生一个或多个错误。 (无法反序列化当前JSON 数组(例如[1,2,3])放入类型,因为该类型需要JSON 对象(例如{“ name”:“ value”})正确反序列化。要解决这个问题 错误,或者将JSON更改为JSON对象(例如{“ name”:“ value”}) 或将反序列化类型更改为实现的数组或类型 像List这样的集合接口(例如ICollection,IList)可以 从JSON数组反序列化。 JsonArrayAttribute也可以是 添加到类型以强制其从JSON数组反序列化。路径 ”,第1行,位置1。)
答案 0 :(得分:4)
JSON数组对应于C#中的/*
This is a code for NEC Infrared Transmission Protocol Transmitter
NEC specifications are
~ Carrier Frequency is 38kHz
* Logical '0' – a 562.5µs pulse burst followed by a 562.5µs space, with a total transmit time of 1.125ms
* Logical '1' – a 562.5µs pulse burst followed by a 1.6875ms space, with a total transmit time of 2.25ms
- a 9ms leading pulse burst (16 times the pulse burst length used for a logical data bit)
- a 4.5ms space
- the 8-bit address for the receiving device
- the 8-bit logical inverse of the address
- the 8-bit command
- the 8-bit logical inverse of the command
- a final 562.5µs pulse burst to signify the end of message transmission.
Example,
If the code recieved from the data dump from the IRremote library is 0x2FD807F
-0x02 is address
-0xFD is the inverse address
-0x80 is the command
-0x7F is the inverse command
THIS PROGRAM IS A BLOCKING PROGRAM
*/
#define IR 3
#define CarrierFreqInterval 11
void setup() {
pinMode(IR, OUTPUT);
digitalWrite(IR, LOW);
}
void loop() {
// unsigned long start = micros();
transmit(0x02FD807F);
// unsigned long ends = micros();
// unsigned long delta = ends - start;
// Serial.println(delta);
delay(500);
}
void transmit(uint32_t data) {
//Function for transmiting the data
uint32_t bitcount = 0x80000000;
// 9ms pulse burst
for (int i = 0; i < 355; i++) {
digitalWrite(IR, HIGH);
delayMicroseconds(CarrierFreqInterval);
digitalWrite(IR, LOW);
delayMicroseconds(CarrierFreqInterval);
}
// 4.5ms space
delayMicroseconds(4500);
//8bit address,adress inverse,command,command inverse
while ( bitcount != 0b0) {
if ((data & bitcount) == bitcount) {
pulseHIGH();
}
else {
pulseLOW();
}
bitcount = bitcount >> 1;
}
//final pulse burst
for (int i = 0; i < 21; i++) {
digitalWrite(IR, HIGH);
delayMicroseconds(CarrierFreqInterval);
digitalWrite(IR, LOW);
delayMicroseconds(CarrierFreqInterval);
}
}
void pulseHIGH() {
// Pulse 38KHz good for a LOGIC '1'
for (int i = 0; i < 21; i++) {
digitalWrite(IR, HIGH);
delayMicroseconds(CarrierFreqInterval);
digitalWrite(IR, LOW);
delayMicroseconds(CarrierFreqInterval);
}
delay(1);
delayMicroseconds(687.5);
}
void pulseLOW() {
// Pulse 38KHz good for a LOGIC '0'
for (int i = 0; i < 21; i++) {
digitalWrite(IR, HIGH);
delayMicroseconds(CarrierFreqInterval);
digitalWrite(IR, LOW);
delayMicroseconds(CarrierFreqInterval);
}
delayMicroseconds(562.5);
}
,因此,应序列化为List<T>
,而不是序列化为RootObject<Sample[]>
:
List<Sample>
在此处使用JsonConvert.DeserializeObject<List<Sample>>(...);
是错误的,因为:
RootObject<Sample[]>
将具有RootObject<Sample[]>
类型的ApiModels
属性,这显然不是您想要的。