将 JSON 转换为列表

Converting JSON to List

我卡在了一个我确信应该可行的步骤中。我有一个方法(在一个单独的类中)在处理 JSON 之后应该 return 一个列表作为它的值。我将跳过 JSON 配置内容粘贴代码:

    public static dynamic CustInformation(string Identifier)
    {

  //SKIPPED JSON CONFIG STUFF (IT'S WORKING CORRECTLY)

        var result = "";
        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        dynamic d;
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            result = streamReader.ReadToEnd();
        }

       return JsonConvert.DeserializeObject<List<Models.RootObject>>(result);
 }

模型是使用 C# 生成的 Json 转换器:

public class Record
{

    public string idIdentifier { get; set; }
    public string KnowName1 { get; set; }
    public string KnowAddress1 { get; set; }
    public string KnowRelation1 { get; set; }
    public string KnowPhone1 { get; set; }
    public string KnowName2 { get; set; }
    public string KnowAddress2 { get; set; }
    //.....skipped other variables

}


public class RootObject
{
    public List<Record> record { get; set; }
}

我这样调用方法:

 var model = Classes.EndPoint.CustInformation(identifier);

然而我每次都收到这个错误:

 Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type      'System.Collections.Generic.List`1[Models.RootObject]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
  To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change 
 the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
 Path 'record', line 1, position 10.

编辑:JSON

  {
   "record": [
    {
      Identifier": "DQRJO1Q0IQRS",
      "KnowName1": "",
      "KnowAddress1": "",
      "KnowRelation1": "",
      "KnowPhone1": "",
      "KnowName2": "",
      "KnowAddress2": "",
      //.....MORE STYFF
    }
  ]
}

就像我在评论中所说的那样,就像错误消息明确指出的那样,您正在尝试反序列化为根对象列表,但您的 JSON 只是一个根对象,而不是数组。

这就是你的 C# 应该是什么。

return JsonConvert.DeserializeObject<Models.RootObject>(result);