我想创建一个像这样的json:
{' sr_no':' OH009876673''数据':[{'代码':' 0124' ' findex':' 3'},{'代码':' 0126'' findex':&# 39; 5'},{'代码':' 0125'' findex':' 8'}]}
我正在尝试这样做但是得到了错误,因为我使用postData作为dictinory
码
var data = new Dictionary<int, string>();
JavaScriptSerializer jss = new JavaScriptSerializer();
axCZKEM1.EnableDevice(iMachineNumber, false);
Cursor = Cursors.WaitCursor;
axCZKEM1.ReadAllUserID(iMachineNumber);//read all the user information to the memory
axCZKEM1.ReadAllTemplate(iMachineNumber);//read all the users' fingerprint templates to the memory
while (axCZKEM1.SSR_GetAllUserInfo(iMachineNumber, out sdwEnrollNumber, out sName, out sPassword, out iPrivilege, out bEnabled))//get all the users' information from the memory
{
for (idwFingerIndex = 0,i=0; idwFingerIndex < 10; idwFingerIndex++,i++)
{
if (axCZKEM1.GetUserTmpExStr(iMachineNumber, sdwEnrollNumber, idwFingerIndex, out iFlag, out sTmpData, out iTmpLength))//get the corresponding templates string and length from the memory
{
var obj = new Dictionary<string, string>();
obj["code"] = sdwEnrollNumber;
obj["findex"] = idwFingerIndex.ToString();
data[i] = jss.Serialize(obj);
i++;
}
}
}
axCZKEM1.EnableDevice(iMachineNumber, true);
var client = new RestClient();
client.EndPoint = @"";
var postData = new Dictionary<string, string>();
postData["sr_no"] = sr_no;
postData["data"] = data; // error on this line because data is array and trying to assign to string
如何解决这个问题,因为我想要JSON中的对象数组来请求API。
答案 0 :(得分:1)
您可以使用NewtonSoft.Json
序列化对象。由于您已经有预期的json数据样本,因此可以在http://json2csharp.com/
public class Datum
{
public string code { get; set; }
public string findex { get; set; }
}
public class RootObject
{
public string sr_no { get; set; }
public List<Datum> data { get; set; }
}
在代码中使用该类,而不是创建Dictionary
RootObject obj = new RootObject();
obj.sr_no = "OH009876673";
obj.data = new List<Datum>();
......
......
while (axCZKEM1.SSR_GetAllUserInfo(iMachineNumber, out sdwEnrollNumber, out sName, out sPassword, out iPrivilege, out bEnabled))//get all the users' information from the memory
{
for (idwFingerIndex = 0,i=0; idwFingerIndex < 10; idwFingerIndex++,i++)
{
if (axCZKEM1.GetUserTmpExStr(iMachineNumber, sdwEnrollNumber, idwFingerIndex, out iFlag, out sTmpData, out iTmpLength))//get the corresponding templates string and length from the memory
{
//var obj = new Dictionary<string, string>();
//obj["code"] = sdwEnrollNumber;
//obj["findex"] = idwFingerIndex.ToString();
//data[i] = jss.Serialize(obj);
//i++;
obj.data.Add( new Datum() {code = sdwEnrollNumber, findex = idwFingerIndex.ToString()});
}
}
}
axCZKEM1.EnableDevice(iMachineNumber, true);
var client = new RestClient();
client.EndPoint = @"";
var outputJson = JsonConvert.SerializeObject(obj);
答案 1 :(得分:0)
试试这个:
var sr_no = "";
var data =new List<Data>();
JavaScriptSerializer jss = new JavaScriptSerializer();
axCZKEM1.EnableDevice(iMachineNumber, false);
Cursor = Cursors.WaitCursor;
axCZKEM1.ReadAllUserID(iMachineNumber);//read all the user information to the memory
axCZKEM1.ReadAllTemplate(iMachineNumber);//read all the users' fingerprint templates to the memory
while (axCZKEM1.SSR_GetAllUserInfo(iMachineNumber, out sdwEnrollNumber, out sName, out sPassword, out iPrivilege, out bEnabled))//get all the users' information from the memory
{
for (idwFingerIndex = 0, i = 0; idwFingerIndex < 10; idwFingerIndex++, i++)
{
if (axCZKEM1.GetUserTmpExStr(iMachineNumber, sdwEnrollNumber, idwFingerIndex, out iFlag, out sTmpData, out iTmpLength))//get the corresponding templates string and length from the memory
{
data.Add(new Data { code = sdwEnrollNumber, findex = idwFingerIndex.ToString() });
i++;
}
}
}
var postObject = new RootObject
{
sr_no = sr_no,
data = data
};
var postData = jss.Serialize(postObject);
public class Data
{
public string code { get; set; }
public string findex { get; set; }
}
public class RootObject
{
public string sr_no { get; set; }
public List<Data> data { get; set; }
}
我还建议使用newtonsoft
代替JavaScriptSerializer
。