我正在寻找一种解决方案,将json响应绑定到winform中的datagrid。我过去3天一直在努力,但进展顺利。
我对json的c#类
public class RootObject
{
public List<string> uid { get; set; }
public List<string> email { get; set; }
public List<string> join { get; set; }
public List<string> mobile { get; set; }
public List<string> name { get; set; }
public List<string> state { get; set; }
public List<string> district { get; set; }
public List<string> address { get; set; }
public List<string> pincode { get; set; }
public List<string> balance { get; set; }
public bool error { get; set; }
public string error_msg { get; set; }
}
我的json回复 -
{"uid":["58924a301801e2.01873036","58c12008715ac3.83978983","58c2c524060d34.25876386","58c61faae8abd4.23607623"],"email":["ing@gmail.com","lemarg@gmail.com","aji@gmail.com","grt@gmail.com"],"join":["2017-02-02 02:20:56","2017-03-09 09:27:40","2017-03-10 15:24:25","2017-03-13 00:00:00"],"mobile":["2147483647","2147483647","214748897","7002352089"],"name":["saurabh saikia","RATUL BHUYAN","AZHAR UDDIN","mon saikia"],"state":["assam","assam","ASSAM","assam"],"district":["jorhat","Jorhat","NAGAON","jrt"],"address":["jorhat","Jorhat","BAGARIGURI, JURIA","jrt"],"pincode":["785001","785001","785001","123456"],"balance":["1.4500","0.0000","0.0000","0.0000"],"error":false,"error_msg":"User Sucessfully Registred "}
在调用Web服务后按钮单击事件我反序列化响应并尝试使用 -
添加到datagridvar jsonlist = JsonConvert.DeserializeObject<RootObject>(jsonResponse);
DataRow rows = this.USER_TABLE.NewRow();
USER_TABLE.Rows.Add(jsonlist.uid.ToArray());
USER_TABLE.Rows.Add(jsonlist.email.ToArray());
USER_TABLE.Rows.Add(jsonlist.join.ToArray());
USER_TABLE.Rows.Add(jsonlist.mobile.ToArray());
USER_TABLE.Rows.Add(jsonlist.name.ToArray());
USER_TABLE.Rows.Add(jsonlist.state.ToArray());
USER_TABLE.Rows.Add(jsonlist.district.ToArray());
USER_TABLE.Rows.Add(jsonlist.address.ToArray());
USER_TABLE.Rows.Add(jsonlist.pincode.ToArray());
USER_TABLE.Rows.Add(jsonlist.balance.ToArray());
我正在附加输出 - DataGridView with data
答案 0 :(得分:0)
将数据添加到DataTable的代码不正确。每行应包含每个List属性
中的一个元素var jsonlist = JsonConvert.DeserializeObject<RootObject>(jsonResponse);
for(int i=0; i < jsonlist.uid.Count; i++)
USER_TABLE.Rows.Add(
jsonlist.uid[i],
jsonlist.email[i],
jsonlist.join[i],
jsonlist.mobile[i],
jsonlist.name[i],
jsonlist.state[i],
jsonlist.district[i],
jsonlist.address[i],
jsonlist.pincode[i],
jsonlist.balance[i]);