如何使用JSON.NET

时间:2015-08-17 15:36:00

标签: c# json asp.net-mvc asp.net-mvc-5 json.net

我正在开发一个asp.net mvc Web应用程序。我收到以下json对象: -

{  
   "operation":{  
      "name":"GET RESOURCE ACCOUNTLIST",
      "result":{  
         "status":"Success",
         "message":"Resource details with account list fetched successfully"
      },
      "Details":{  
         "RESOURCE ID":"1",
         "RESOURCE NAME":"test resource",
         "RESOURCE DESCRIPTION":"",
         "RESOURCE TYPE":"Windows",
         "DNS NAME":"172.16.20.101",
         "PASSWORD POLICY":"Strong",
         "DEPARTMENT":"",
         "LOCATION":"",
         "RESOURCE URL":"",
         "RESOURCE OWNER":"admin",
         "ACCOUNT LIST":[  
            {  
               "ISFAVPASS":"false",
               "ACCOUNT NAME":"root",
               "PASSWDID":"1",
               "IS_TICKETID_REQD_MANDATORY":"false",
               "ISREASONREQUIRED":"false",
               "AUTOLOGONLIST":[  
                  "Windows Remote Desktop",
                  "Remote Desktop"
               ],
               "PASSWORD STATUS":"****",
               "IS_TICKETID_REQD":"false",
               "ACCOUNT ID":"1",
               "AUTOLOGONSTATUS":"User is not allowed to automatically logging in to remote systems in mobile",
               "IS_TICKETID_REQD_ACW":"false"
            }
         ]
      }
   }
}

我正在使用JSON.NET进行deserilization,所以我创建了以下模型类(我没有包含JSON对象收到的所有属性,因为我并不真正需要它们)。

public class ResourceAccountListInfo
{
    public Operation2 operation { get; set; }
}

public class Operation2
{    
    public string name { get; set; }
    public Result result { get; set; }
    public IList<Details2> Details { get; set; }    
}

public class Details2
{     
    [JsonProperty("RESOURCE DESCRIPTION")]
    public string RESOURCEDESCRIPTION { get; set; }

    [JsonProperty("RESOURCE NAME")]
    public string RESOURCENAME { get; set; }

    [JsonProperty("RESOURCE ID")]
    public string RESOURCEID { get; set; }

    [JsonProperty("RESOURCE TYPE")]
    public string RESOURCETYPE { get; set; }

    [JsonProperty("DNS NAME")]
    public string DNSNAME { get; set; }

    [JsonProperty("ACCOUNT LIST")]
    public IList<ACCOUNTLIST> ACCOUNTLIST { get; set; }
}

当我尝试使用以下语句反序列化json时,我收到以下错误:

  ResourceAccountListInfo resourceAccountListInfo = JsonConvert.DeserializeObject<ResourceAccountListInfo>(json); 

错误是:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IList`1[T.ViewModels.Details2]' 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 'operation.Details.['RESOURCE ID']', line 1, position 171

我认为问题是我的JSON对象中有一些属性在模型类中没有映射,这可能是一个原因吗?

2 个答案:

答案 0 :(得分:2)

这里的错误信息实际上很有启发性:

  

无法将当前JSON对象(例如{“name”:“value”})反序列化为类型'System.Collections.Generic.IList`1 [T.ViewModels.Details2]',因为该类型需要JSON数组(例如[1,2,3])正确反序列化。   要修复此错误,请将JSON更改为JSON数组(例如[1,2,3])或更改反序列化类型,使其成为普通的.NET类型(例如,不是像整数这样的基本类型,而不是类似的集合类型可以从JSON对象反序列化的数组或List。 JsonObjectAttribute也可以添加到类型中以强制它从JSON对象反序列化。   路径'operation.Details。['RESOURCE ID']',第1行,第171位

它告诉你的是它有一个对象,你要求它将它反序列化为一个集合。它不能这样做,因为它不是一个开始的集合。

你的模特中有:

public IList<Details2> Details { get; set; }

但是你的json中相应的details属性是:

  "Details":{  
     "RESOURCE ID":"1",
     "RESOURCE NAME":"test resource",
     "RESOURCE DESCRIPTION":"",
     "RESOURCE TYPE":"Windows",
     "DNS NAME":"172.16.20.101",
     "PASSWORD POLICY":"Strong",
     "DEPARTMENT":"",
     "LOCATION":"",
     "RESOURCE URL":"",
     "RESOURCE OWNER":"admin",
     "ACCOUNT LIST":[  
        {  
           "ISFAVPASS":"false",
           "ACCOUNT NAME":"root",
           "PASSWDID":"1",
           "IS_TICKETID_REQD_MANDATORY":"false",
           "ISREASONREQUIRED":"false",
           "AUTOLOGONLIST":[  
              "Windows Remote Desktop",
              "Remote Desktop"
           ],
           "PASSWORD STATUS":"****",
           "IS_TICKETID_REQD":"false",
           "ACCOUNT ID":"1",
           "AUTOLOGONSTATUS":"User is not allowed to automatically logging in to remote systems in mobile",
           "IS_TICKETID_REQD_ACW":"false"
        }
     ]
  }

哪个是对象(请注意大括号{})。

所以您需要做的就是将Details属性更改为:

public Details2 Details { get; set; }

json details中没有Details2中相应属性的任何属性都将被忽略。因此,不需要映射json的每个属性,只映射您真正关心的属性。

答案 1 :(得分:0)

您可以将JObjectdynamic

一起使用
dynamic json = JObject.Parse(jsonString);

然后按照您想要的方式填充模型,而不创建整个类层次结构:

var operationName = json.operation.name;

请注意:使用dynamic您的属性将在运行时创建。这意味着您将不会获得智能感知支持,如果您尝试调用不存在的方法或属性,您将获得运行时异常。所以,要非常小心这种方法。