Using JSON content using LINQ / C #

I am trying to process multiple files and move their data to a SharePoint list. I totally agree with the SharePoint component of my request, but I'm struggling to wrap my head around accepting JSON and executing C # statements against it.

The JSON I'm working with is in the following format:

{
  "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
    }
  ]
}

Based on this StackExchange request I tried to create a class to deserialize my content using the following:

var des = (ResourceOrdersJSONEntity)Newtonsoft.Json.JsonConvert.DeserializeObject(sampleJSON, typeof(ResourceOrdersJSONEntity));

This does not work for me, and I suspect that I am looking at a completely wrong path. What do I need to know about to take JSON and wrap linq expressions around it for processing in SharePoint?

The error I get is:

JSON (, { "name": "value" }) 'System.Collections.Generic.List`1

, JSON JSON (, [1,2,3]) , .NET. type (, integer, ), JSON. JsonObjectAttribute , JSON.

, , , , , . , .

, :

   ResourceOrdersJSONEntity   {       public List Details {get; ; }        {get; ; }   }

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; }
}

   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; }
}
+4
3

, , ResourceOrdersJSONEntity :

public class ResourceOrdersJSONEntity
{
    public ResourceOrdersDetailsEntity Details { get; set; }
    public List<ResourceOrdersQuantityEntity> Orders { get; set; }
}
+3

JsonConvert.DeserializeObject<ResourceOrdersJSONEntity>(sampleJson). JSON .

.

public class ResourceOrdersJSONEntity
{
    public ResourceOrdersDetailsEntity Details { get; set; }
    public List<ResourceOrdersQuantityEntity> Orders { get; set; }
}

JSON {}, ( // JSON) []. JSON #, JSON #/array/collection. JSON.net , "details" List

+2

Your ResourceOrdersJSONEntity class should look like this:

public class ResourceOrdersJSONEntity 
{ 
   public List Details { get; set; } 
   public List<ResourceOrdersQuantityEntity> Orders { get; set; } 
}
0
source

Source: https://habr.com/ru/post/1599015/


All Articles