C#:JSON DeserializeObject给出异常

时间:2015-01-20 20:38:12

标签: c# json

我有一个如下所示的JSON字符串,我从网络服务获取:

"[[\"XYZ40E\",\"D        XYZ-12572\",\"XYZ, XYZ HARDWARE END ITEM, XYZ40 (REV D) IXYZ SE KPD\"],[\"5.001.0.045\",\"M        XYZ-14280\",\"PROGRAMMING AND CONFIGURING IXYZ SE XYZ\"],[\"222TTEK0000\",\"A        XYZ-14631\",\"XYZ, XYZ40, MULTIXYZ, SE REV D, XYZ, STD XYZ, STD, XYZ, PIG, BLK, STD 1 SECURITY, LED RED, FLASH GRN, BZR ON, IPM OFF, KPF-4-BIT, 32 BIT\"],[\"4505-104-10\",\"E        XYZ-10728\",\"LBL, PROD, MULTICLASS SE XYZ40, REV D XYZ\"],[\"4505-105-10\",\"D        XYZ-10850\",\"LBL, XYZ SIDE SHPNG, MULTIXYZ SE XYZ40, REV D XYZ\"],[\"XYZ-GRPC-003N\",\"B        XYZ-11880\",\"CFG PFL, LF STD, HF XYZ\\/SIO\\/XYZ, XYZG, LED RED, FLXYZ GRN, BZR ON, CSN 34-BIT LXYZ, KPF, BXYZRD 1 KEY, NO PAR, 4-BIT MSG, IPM OFF\"]]"

现在我使用的代码是:

var retVal = JsonConvert.DeserializeObject<List<string>>(string);

此处字符串是上面显示的。

我得到了例外:

Error reading string. Unexpected token: StartArray. Path '[0]', line 1, position 2.

为什么我得到这个例外?我对JSON很陌生。

2 个答案:

答案 0 :(得分:5)

因为您的模型属于List<List<string>>类型。

enter image description here

答案 1 :(得分:0)

查看here,然后尝试

string str = "[[\"XYZ40E\",\"D        XYZ-12572\",\"XYZ, XYZ HARDWARE END ITEM, XYZ40 (REV D) IXYZ SE KPD\"],[\"5.001.0.045\",\"M        XYZ-14280\",\"PROGRAMMING AND CONFIGURING IXYZ SE XYZ\"],[\"222TTEK0000\",\"A        XYZ-14631\",\"XYZ, XYZ40, MULTIXYZ, SE REV D, XYZ, STD XYZ, STD, XYZ, PIG, BLK, STD 1 SECURITY, LED RED, FLASH GRN, BZR ON, IPM OFF, KPF-4-BIT, 32 BIT\"],[\"4505-104-10\",\"E        XYZ-10728\",\"LBL, PROD, MULTICLASS SE XYZ40, REV D XYZ\"],[\"4505-105-10\",\"D        XYZ-10850\",\"LBL, XYZ SIDE SHPNG, MULTIXYZ SE XYZ40, REV D XYZ\"],[\"XYZ-GRPC-003N\",\"B        XYZ-11880\",\"CFG PFL, LF STD, HF XYZ\\/SIO\\/XYZ, XYZG, LED RED, FLXYZ GRN, BZR ON, CSN 34-BIT LXYZ, KPF, BXYZRD 1 KEY, NO PAR, 4-BIT MSG, IPM OFF\"]]"
var retVal = JsonConvert.DeserializeObject<List<List<string>>>(str);