C# 使用LINQ/C消费JSON内容#

C# 使用LINQ/C消费JSON内容#,c#,json,linq,C#,Json,Linq,我正在尝试处理大量文件,并将其数据移动到SharePoint列表中。我对请求中的SharePoint组件非常满意,但我很难接受JSON并对其执行C#语句 我使用的JSON格式如下: { "details": { "persona": "jdfh sjahfj ashd", "firstName": "difdfsdfh", "surname": "safasfjhgfasdf", "email": "dfashfjsahf@dfjhdfjhsd.com",

我正在尝试处理大量文件,并将其数据移动到SharePoint列表中。我对请求中的SharePoint组件非常满意,但我很难接受JSON并对其执行C#语句

我使用的JSON格式如下:

{
  "details": {
    "persona": "jdfh sjahfj ashd",
    "firstName": "difdfsdfh",
    "surname": "safasfjhgfasdf",
    "email": "dfashfjsahf@dfjhdfjhsd.com",
    "phone": "3256545 421",
    "organisation": "dsfhjafd gas gdf",
    "department": "hsagfhsdgahjf",
    "address": {
      "street": "11/338 shjafg ash fg",
      "suburb": "gsagfghasf",
      "postcode": "4006"
    },
    "questions": {
      "iAmA": "fhsahjfsajhd df as",
      "iWorkAtA": "",
      "iAmSeekingFor": "ahshfjgsfggdh",
      "iAmAMemberOf": "dafjksfhdh",
      "furtherInfoOptIn": true,
      "newsletterOptIn": false,
      "privacyCollection": true
    }
  },
  "orders": [
    {
      "name": "Business Cards",
      "quantity": 8
    },
    {
      "name": "QUITLINE",
      "quantity": 5
    }
  ]
}
根据StackExchange请求,我尝试创建一个类来反序列化我的内容,以使用以下内容:

var des = (ResourceOrdersJSONEntity)Newtonsoft.Json.JsonConvert.DeserializeObject(sampleJSON, typeof(ResourceOrdersJSONEntity));
这对我来说不起作用,我怀疑我在寻找一条完全错误的道路。为了获取JSON并将其周围的linq语句封装到SharePoint中进行处理,我需要学习什么

我收到的错误是:

无法反序列化当前JSON对象(例如{“名称”:“值”}) 输入类型“System.Collections.Generic.List`1”

要修复此错误,请将JSON更改为JSON数组(例如。 [1,2,3])或更改反序列化类型,使其成为普通的.NET 类型(例如,不是integer之类的基元类型,也不是集合类型 类似于数组或列表),可以从JSON对象反序列化。 还可以将JsonObjectAttribute添加到类型以强制其 从JSON对象反序列化

我不太明白这对我意味着什么,我确信这是我的类结构的问题,但我需要一些关于如何修复它的指导

以下是我为反序列化创建的类:

主要实体 公共类ResourceOrdersJSONEntity { 公共列表详细信息{get;set;} 公共列表顺序{get;set;} }

详细信息

public class ResourceOrdersDetailsEntity
{
    /// <summary>
    /// Gets or sets the persona.
    /// </summary>
    public string Persona { get; set; }

    /// <summary>
    /// Gets or sets the first name.
    /// </summary>
    public string FirstName { get; set; }

    /// <summary>
    /// Gets or sets the surname.
    /// </summary>
    public string Surname { get; set; }

    /// <summary>
    /// Gets or sets the email.
    /// </summary>
    public string Email { get; set; }

    /// <summary>
    /// Gets or sets the phone.
    /// </summary>
    public string Phone { get; set; }

    /// <summary>
    /// Gets or sets the organisation.
    /// </summary>
    public string Organisation { get; set; }

    /// <summary>
    /// Gets or sets the department.
    /// </summary>
    public string Department { get; set; }

    /// <summary>
    /// Gets or sets the address.
    /// </summary>
    public ResourceOrdersAddressEntity Address { get; set; }

    /// <summary>
    /// Gets or sets the questions.
    /// </summary>
    public ResourceOrdersQuestionsEntity Questions { get; set; }
}
公共类资源顺序详细信息
{
/// 
///获取或设置角色。
/// 
公共字符串角色{get;set;}
/// 
///获取或设置第一个名称。
/// 
公共字符串名{get;set;}
/// 
///获取或设置姓氏。
/// 
公共字符串姓氏{get;set;}
/// 
///获取或设置电子邮件。
/// 
公共字符串电子邮件{get;set;}
/// 
///获取或设置电话。
/// 
公用字符串电话{get;set;}
/// 
///获取或设置组织。
/// 
公共字符串组织{get;set;}
/// 
///获取或设置部门。
/// 
公共字符串部门{get;set;}
/// 
///获取或设置地址。
/// 
公共资源OrdersAddress属性地址{get;set;}
/// 
///获取或设置问题。
/// 
public ResourceOrderQuestionsEntity问题{get;set;}
}
地址实体

   public class ResourceOrdersAddressEntity
    {
        /// <summary>
        /// Gets or sets the street.
        /// </summary>
        public string Street { get; set; }

        /// <summary>
        /// Gets or sets the suburb.
        /// </summary>
        public string Suburb { get; set; }

        /// <summary>
        /// Gets or sets the postcode.
        /// </summary>
        public string Postcode { get; set; }
    }
public class ResourceOrdersQuestionsEntity
{
/// <summary>
/// Gets or sets the i ama.
/// </summary>
public string IAma { get; set; }

/// <summary>
/// Gets or sets the i work at a.
/// </summary>
public string IWorkAtA { get; set; }

/// <summary>
/// Gets or sets the i am seeking for.
/// </summary>
public string IAmSeekingFor { get; set; }

/// <summary>
/// Gets or sets the i am a member for.
/// </summary>
public string IAmAMemberFor { get; set; }

/// <summary>
/// Gets or sets the further info opt in.
/// </summary>
public string FurtherInfoOptIn { get; set; }

/// <summary>
/// Gets or sets the news letter opt in.
/// </summary>
public string NewsLetterOptIn { get; set; }

/// <summary>
/// Gets or sets the privacy collection.
/// </summary>
public string PrivacyCollection { get; set; }
}
public class ResourceOrdersQuantityEntity
{
    /// <summary>
    /// Gets or sets the name.
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// Gets or sets the quantity.
    /// </summary>
    public int Quantity { get; set; }
}
公共类ResourceOrdersAddressity
{
/// 
///获取或设置街道。
/// 
公共字符串Street{get;set;}
/// 
///获取或设置郊区。
/// 
公共字符串{get;set;}
/// 
///获取或设置邮政编码。
/// 
公共字符串邮政编码{get;set;}
}
问题实体

   public class ResourceOrdersAddressEntity
    {
        /// <summary>
        /// Gets or sets the street.
        /// </summary>
        public string Street { get; set; }

        /// <summary>
        /// Gets or sets the suburb.
        /// </summary>
        public string Suburb { get; set; }

        /// <summary>
        /// Gets or sets the postcode.
        /// </summary>
        public string Postcode { get; set; }
    }
public class ResourceOrdersQuestionsEntity
{
/// <summary>
/// Gets or sets the i ama.
/// </summary>
public string IAma { get; set; }

/// <summary>
/// Gets or sets the i work at a.
/// </summary>
public string IWorkAtA { get; set; }

/// <summary>
/// Gets or sets the i am seeking for.
/// </summary>
public string IAmSeekingFor { get; set; }

/// <summary>
/// Gets or sets the i am a member for.
/// </summary>
public string IAmAMemberFor { get; set; }

/// <summary>
/// Gets or sets the further info opt in.
/// </summary>
public string FurtherInfoOptIn { get; set; }

/// <summary>
/// Gets or sets the news letter opt in.
/// </summary>
public string NewsLetterOptIn { get; set; }

/// <summary>
/// Gets or sets the privacy collection.
/// </summary>
public string PrivacyCollection { get; set; }
}
public class ResourceOrdersQuantityEntity
{
    /// <summary>
    /// Gets or sets the name.
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// Gets or sets the quantity.
    /// </summary>
    public int Quantity { get; set; }
}
公共类ResourceOrderQuestionsEntity
{
/// 
///获取或设置i值。
/// 
公共字符串IAma{get;set;}
/// 
///获取或设置我在a工作的时间。
/// 
公共字符串IWorkAtA{get;set;}
/// 
///获取或设置我正在查找的。
/// 
{get;set;}的公共字符串
/// 
///获取或设置我是其成员的。
/// 
{get;set;}的公共字符串IAAMEMBERFOR
/// 
///获取或设置进一步的信息选择加入。
/// 
公共字符串信息选项{get;set;}
/// 
///获取或设置新闻信函选择加入。
/// 
公共字符串{get;set;}
/// 
///获取或设置隐私集合。
/// 
公共字符串PrivacyCollection{get;set;}
}
订单实体

   public class ResourceOrdersAddressEntity
    {
        /// <summary>
        /// Gets or sets the street.
        /// </summary>
        public string Street { get; set; }

        /// <summary>
        /// Gets or sets the suburb.
        /// </summary>
        public string Suburb { get; set; }

        /// <summary>
        /// Gets or sets the postcode.
        /// </summary>
        public string Postcode { get; set; }
    }
public class ResourceOrdersQuestionsEntity
{
/// <summary>
/// Gets or sets the i ama.
/// </summary>
public string IAma { get; set; }

/// <summary>
/// Gets or sets the i work at a.
/// </summary>
public string IWorkAtA { get; set; }

/// <summary>
/// Gets or sets the i am seeking for.
/// </summary>
public string IAmSeekingFor { get; set; }

/// <summary>
/// Gets or sets the i am a member for.
/// </summary>
public string IAmAMemberFor { get; set; }

/// <summary>
/// Gets or sets the further info opt in.
/// </summary>
public string FurtherInfoOptIn { get; set; }

/// <summary>
/// Gets or sets the news letter opt in.
/// </summary>
public string NewsLetterOptIn { get; set; }

/// <summary>
/// Gets or sets the privacy collection.
/// </summary>
public string PrivacyCollection { get; set; }
}
public class ResourceOrdersQuantityEntity
{
    /// <summary>
    /// Gets or sets the name.
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// Gets or sets the quantity.
    /// </summary>
    public int Quantity { get; set; }
}
公共类ResourceOrdersQuantityEntity
{
/// 
///获取或设置名称。
/// 
公共字符串名称{get;set;}
/// 
///获取或设置数量。
/// 
公共整数数量{get;set;}
}

尝试使用
JsonConvert.DeserializeObject(sampleJson)
方法。它将JSON强制转换到指定的类

如下文所述。您必须将主实体更改为此

public class ResourceOrdersJSONEntity
{
    public ResourceOrdersDetailsEntity Details { get; set; }
    public List<ResourceOrdersQuantityEntity> Orders { get; set; }
}
公共类ResourceOrdersJSONEntity
{
公共资源订单详细信息{get;set;}
公共列表顺序{get;set;}
}

在JSON中,对象用花括号括起来,数组(这是JSON中唯一的列表/数组/集合类型)用方括号括起来。JSON对象需要转换为C对象,JSON数组需要转换为C列表/数组/集合。这就是为什么当您试图将
“details”
对象转换为
列表时,JSON.net会给您一个错误的原因。您的ResourceOrdersJSONEntity类应该是这样的:

public class ResourceOrdersJSONEntity 
{ 
   public List Details { get; set; } 
   public List<ResourceOrdersQuantityEntity> Orders { get; set; } 
}
公共类ResourceOrdersJSONEntity
{ 
公共列表详细信息{get;set;}
公共列表顺序{get;set;}
}

我只需将
ResourceOrdersJSONEntity
更改为:

public class ResourceOrdersJSONEntity
{
    public ResourceOrdersDetailsEntity Details { get; set; }
    public List<ResourceOrdersQuantityEntity> Orders { get; set; }
}
公共类ResourceOrdersJSONEntity
{
公共资源订单详细信息{get;set;}
公共列表顺序{get;set;}
}

您是否尝试过这个var contactPerson=JsonConvert.DeserializeObject(sampleJSON);看来你走对了路。你能为class
ResourceOrdersJSONEntity
粘贴代码吗?另外,当你说它不工作时,到底是什么问题?@Light我刚刚用它更新了代码,但仍然有问题。我怀疑我的课程是问题所在(现在添加到问题中)?@MikeHixson添加到问题中!抱歉,我坐在记事本上,在删除公司数据后,我忘了把它带过来。我已经做了这个更改,但仍然收到一个错误-添加了类和问题的例外,因为看起来我在正确的轨道上,但在常量的某个地方出错