杰森:
[
  {
    "id": 1,
    "name": "Company",
    "lID": 1,
    "uuid": "56cd87e48528e",
  }
]类:
public class details
{
    public int id { get; set; }
    public string name { get; set; }
    public int lID { get; set; }
    public string uuid { get; set; }
}
private void button1_Click(object sender, EventArgs e){
    var json = new WebClient().DownloadString("url");
    details jsonconvert = JsonConvert.DeserializeObject<details>(json);
    string s = JsonConvert.SerializeObject(jsonconvert.uuid);
    /* foreach (var item in jsonconvert){
        if (textBox1.Text == s){
            Form2 secondForm = new Form2();
            secondForm.Show();
        }else{
            MessageBox.Show("Company not found.");
        }
    }*/
}这就是结果/问题:
附加信息:无法将当前JSON数组(例如,1,2,3)反序列化为'WindowsFormsApplication1.Form1+details‘类型,因为该类型需要一个JSON对象(例如{“名称”:“值”})才能正确反序列化。 要修复此错误,要么将JSON更改为JSON对象(例如
{"name":"value"}),要么将反序列化类型更改为数组,或者将实现集合接口(例如ICollection、IList)的类型改为可以从JSON数组反序列化的列表。还可以将JsonArrayAttribute添加到该类型中,以强制它从JSON数组中反序列化。 路径'',第1行,位置1。
发布于 2016-05-19 13:04:07
要修复此错误,要么将JSON更改为JSON对象(例如{“名称”:“值”}),要么将反序列化类型更改为数组或实现集合接口的类型(例如ICollection、IList)
details jsonconvert = JsonConvert.DeserializeObject<details>(json);中,json转换不是List类型,因此将json数组转换为对象将失败。因此,类“细节”必须实现ICollection或IList等。https://stackoverflow.com/questions/37323685
复制相似问题