I'm trying to process a number of files and move their data into a SharePoint List. I'm completely comfortable with the SharePoint component of my request but I'm struggling to wrap my head around taking JSON and performing 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 upon this StackExchange request I've tried creating a class to deserialize my content into using the following:
var des = (ResourceOrdersJSONEntity)Newtonsoft.Json.JsonConvert.DeserializeObject(sampleJSON, typeof(ResourceOrdersJSONEntity));
This isn't working for me and I suspect I'm looking down the completely wrong path. What do I need to be learning about in order to take the JSON and wrap linq statements around it for processing into SharePoint?
The error I'm receiving is:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1
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) 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.
I don't quite understand what it's indicating to me and I'm sure it's my class structure that's the problem, but I need a bit of guidance on how to fix it.
Here are the classes I've created for deserialization:
Main Entity public class ResourceOrdersJSONEntity { public List Details { get; set; } public List Orders { get; set; } }
Details
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; }
}
Address Entity
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; }
}
Questions Entity
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; }
}
Orders Entity
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; }
}
>(sampleJSON);
– Light Jul 22 '15 at 05:46