Xamarin Forms Json Desrialization - json

I want to deserialize a JSON coming from a web service, but for some reason I am getting an exception. I searched every question related to this topic but I did not find anything related to this exception. What might me the problem? here is the exception:
Newtonsoft.Json.JsonSerializationException: 'Unexpected token while deserializing object: EndObject. Path '', line 1, position 243.'
Here is my deserialize method:
public async Task<LoginApiResponse> AuthenticateUserAsync(string username, string password)
{
// try
// {
LoginApiRequest loginRequest = new LoginApiRequest()
{
Username = username,
Password = password
};
// serialize object to json
var content = new StringContent(JsonConvert.SerializeObject(loginRequest), Encoding.UTF8, "application/json");
var response = await Client.PostAsync(Constants.LOGIN_URI, content);
response.EnsureSuccessStatusCode();
// get the data on success and serialize it from json
using (var stream = await response.Content.ReadAsStreamAsync())
using (var reader = new StreamReader(stream))
using (var json = new JsonTextReader(reader))
{
System.Diagnostics.Debug.Write(">>>>>>>>>>>>>>>>>>>> " + Serializer.Deserialize<LoginApiResponse>(json));
return Serializer.Deserialize<LoginApiResponse>(json);
}
// }
/* catch (Exception ex)
{
return null;
}*/
}
here is the Request model:
public class LoginApiRequest
{
[JsonProperty("Username")]
public string Username { get; set; }
[JsonProperty("Password")]
public string Password { get; set; }
public LoginApiRequest() { }
}
here is the Response Model:
public class LoginApiResponse : MessageStatus
{
[JsonProperty("Data")]
public User Data { get; set; }
public LoginApiResponse() { }
}
here is the User model:
public class User
{
[JsonProperty("Address")]
public string Address { get; set; }
[JsonProperty("BloodType")]
public string BloodType { get; set; }
[JsonProperty("ConfirmPassword")]
public string ConfirmPassword { get; set; }
[JsonProperty("CustomerID")]
public string CustomerID { get; set; }
[JsonProperty("DOB")]
public string DOB { get; set; }
[JsonProperty("Email")]
public string Email { get; set; }
[JsonProperty("ID")]
public int? ID { get; set; }
[JsonProperty("IsActive")]
public bool IsActive { get; set; }
[JsonProperty("Name")]
public string Name { get; set; }
[JsonProperty("Password")]
public string Password { get; set; }
[JsonProperty("Phone")]
public string Phone { get; set; }
[JsonProperty("UserRoleID")]
public int? UserRoleID { get; set; }
[JsonProperty("Username")]
public string Username { get; set; }
public User() { }
}
and here is the the messagestatus:
public class MessageStatus
{
[JsonProperty("Message")]
public string Message { get; set; }
[JsonProperty("Status")]
public int Status { get; set; }
public MessageStatus() { }
}
and finally here is the json:
{
"Message": null,
"Status": 1,
"Data": {
"Address": "Beirut",
"BloodType": null,
"ConfirmPassword": null,
"CustomerID": null,
"DOB": null,
"Email": null,
"ID": 22,
"IsActive": true,
"Name": "tg",
"Password": "123456",
"Phone": "03708424",
"UserRoleID": 1,
"Username": "tg"
}
}

instead of
using (var stream = await response.Content.ReadAsStreamAsync())
using (var reader = new StreamReader(stream))
using (var json = new JsonTextReader(reader))
{
System.Diagnostics.Debug.Write(">>>>>>>>>>>>>>>>>>>> " + Serializer.Deserialize<LoginApiResponse>(json));
return Serializer.Deserialize<LoginApiResponse>(json);
}
try
var json = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject<LoginApiResponse>(json)
return data;

Related

RestSharp query returning NotFound

This is my first usage of RestSharp
I am trying to connect to HubSpot using their FormsAPI (https://legacydocs.hubspot.com/docs/methods/forms/submit_form)
Using .Net, C#, MVC.
When I run in Fiddler, it works.
Here is my C# code, when I run it, I get a StatusCode of "NotFound". I am sure it is something simple I am missing?
var client = new RestClient("https://api.hsforms.com");
var request = new RestRequest("submissions/v3/integration/submit/{PortalId}/{formGuid}", Method.POST);
request.AddUrlSegment("portalId", "[myportalid]");
request.AddUrlSegment("formGuid", "[myformid]");
request.AddQueryParameter("hapikey", "[myapikey]");
request.RequestFormat = DataFormat.Json;
request.AddParameter("firstname", "testfirstname");
request.AddParameter("lastname", "testlastname");
request.AddParameter("email", "testemail#emailaddress.com");
request.AddParameter("business_unit", "Test");
It is better to create model of c# class and serialize it to Json and send POST request.
Example of RestSharp request
public async Task SendHubSpotRequest()
{
var PortalId = 1;
var formGuid = 1;
var client = new RestClient("https://api.hsforms.com");
var request = new RestRequest($"submissions/v3/integration/submit/{PortalId}/{formGuid}", Method.POST);
var hubSpotRequest = new HubSpotRequest()
{
SubmittedAt = "1517927174000",
Fields = new Field[]
{
new Field() { Name = "email", Value = "testemail#emailaddress.com" },
new Field() { Name = "firstname", Value = "testfirstname" },
new Field() { Name = "lastname", Value = "testlastname" }
},
Context = new Context
{
Hutk = "hutk",
PageUri = "www.example.com/page",
PageName = "Example page"
},
LegalConsentOptions = new LegalConsentOptions
{
Consent = new Consent
{
// Fill other params
}
}
};
request.AddParameter("application/json; charset=utf-8", JsonConvert.SerializeObject(hubSpotRequest), ParameterType.RequestBody);
var response = await client.ExecuteAsync(request);
var responseContent = response.Content;
}
C# classes of body json model
public class HubSpotRequest
{
[JsonProperty("submittedAt")]
public string SubmittedAt { get; set; }
[JsonProperty("fields")]
public Field[] Fields { get; set; }
[JsonProperty("context")]
public Context Context { get; set; }
[JsonProperty("legalConsentOptions")]
public LegalConsentOptions LegalConsentOptions { get; set; }
}
public class Context
{
[JsonProperty("hutk")]
public string Hutk { get; set; }
[JsonProperty("pageUri")]
public string PageUri { get; set; }
[JsonProperty("pageName")]
public string PageName { get; set; }
}
public class Field
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
}
public class LegalConsentOptions
{
[JsonProperty("consent")]
public Consent Consent { get; set; }
}
public class Consent
{
[JsonProperty("consentToProcess")]
public bool ConsentToProcess { get; set; }
[JsonProperty("text")]
public string Text { get; set; }
[JsonProperty("communications")]
public Communication[] Communications { get; set; }
}
public class Communication
{
[JsonProperty("value")]
public bool Value { get; set; }
[JsonProperty("subscriptionTypeId")]
public long SubscriptionTypeId { get; set; }
[JsonProperty("text")]
public string Text { get; set; }
}

Get item from JSON Odata list for UWP

I have been having a hard time trying to figure this out that I've just about torn out all my hair now.
Using this section of code (Added a var result that I looking at with a stoppoint):
public async Task<string> GetHttpSPContentWithToken(string url, string token)
{
var httpClient = new System.Net.Http.HttpClient();
System.Net.Http.HttpResponseMessage response;
try
{
var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, url);
//Add the token in Authorization header
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
response = await httpClient.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<SharePointListItems.Fields>(content);
return content;
}
catch (Exception ex)
{
return ex.ToString();
}
}
The content that I receive is this (updated getting rid of extra information):
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites('root')/lists('FBA0AB63-8453-4BB9-AA17-142A5D72A50D')/items/$entity",
"#odata.etag": "\"60d40002-0f08-4f29-afa7-0287137b863b,1\"",
"createdDateTime": "2018-08-07T14:28:47Z",
"eTag": "\"60d40002-0f08-4f29-afa7-0287137b863b,1\"",
"id": "1",
"lastModifiedDateTime": "2018-08-07T14:28:47Z",
"webUrl": "https://XXXX.sharepoint.com/Lists/TestList/1_.000",
"createdBy": {
"user": {
"email": "XXXX#XXXX.onmicrosoft.com",
"id": "b5f81cc6-f8b7-46b7-8e10-6ce1b9689c23",
"displayName": "TK"
}
},
"lastModifiedBy": {
"user": {
"email": "XXXX#XXXX.onmicrosoft.com",
"id": "b5f81cc6-f8b7-46b7-8e10-6ce1b9689c23",
"displayName": "TK"
}
},
"parentReference": {},
"contentType": {
"id": "0x010001403BD420356E4ABE3B63E5AEC0713D"
},
"fields#odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites('root')/lists('FBA0AB63-8453-4BB9-AA17-142A5D72A50D')/items('1')/fields/$entity",
"fields": {
"#odata.etag": "\"60d40002-0f08-4f29-afa7-0287137b863b,1\"",
"Title": "1",
"UserName": "TK",
"UserAge": "47",
"UserTitle": "Developer"
}
}
I just want the values forUserAge, UserName, and UserTitle to put each into a textbox, but not sure how to pull them out.
I am pretty sure that I need to set up a class of some sort, but it is the #odata parts that are breaking my back.
Everything that I have tried just gives me back a null value. I see the value there, just not sure how to parse/pull it out.
I have looked at this (updated):
using Newtonsoft.Json;
using System;
public class SharePointListItems
{
public class UserCreated
{
public string email { get; set; }
public string id { get; set; }
public string displayName { get; set; }
}
public class CreatedBy
{
public UserCreated user { get; set; }
}
public class UserModified
{
public string email { get; set; }
public string id { get; set; }
public string displayName { get; set; }
}
public class LastModifiedBy
{
public UserModified user { get; set; }
}
public class ParentReference
{
}
public class ContentType
{
public string id { get; set; }
}
public class Fields
{
[JsonProperty("#odata.etag")]
public string ODataETag { get; set; }
public string Title { get; set; }
public string UserName { get; set; }
public string UserAge { get; set; }
public string UserTitle { get; set; }
}
public class RootObject
{
[JsonProperty("#odata.context")]
public string ODataContext { get; set; }
[JsonProperty("#odata.etag")]
public string ODataETag { get; set; }
public DateTime createdDateTime { get; set; }
public string eTag { get; set; }
public string id { get; set; }
public DateTime lastModifiedDateTime { get; set; }
public string webUrl { get; set; }
public CreatedBy createdBy { get; set; }
public LastModifiedBy lastModifiedBy { get; set; }
public ParentReference parentReference { get; set; }
public ContentType contentType { get; set; }
[JsonProperty("fields#odata.context")]
public string FieldsODataContext { get; set; }
public Fields fields { get; set; }
}
}
But then I run into the issue that there is two [JsonProperty("#odata.etag")].
The [JsonProperty] custom attribute is added to the C# property that actually holds that value. Instead of putting the attribute on the Title or createDateTime property, you need to put them on their own properties:
public class RootObject
{
[JsonProperty("#odata.context")]
public string ODataContext { get; set; }
[JsonProperty("#odata.etag")]
public string ODataETag { get; set; }
// No attribute needed here
public DateTime createdDateTime { get; set; }
// etc...
Also, you are trying to parse the content as a Fields class, but it is a RootObject; you need to use
JsonConvert.DeserializeObject<SharePointListItems.RootObject>(content)
To get the object.

SSIS Script Component JSON DataContractJsonSerializer

Edit - 5/8 Json sample:
{
"data": [
{
"evidenceId": "9999A999-9D99-4411-8819-DD55D9DDD55D",
"status": "Active",
"title": "Video 2017-04-30 2354",
"idExternal": null,
"description": null,
"dateUploaded": "2017-05-01T01:33:10.522-06:00",
"dateModified": "2017-05-01T01:33:10.970-06:00",
"dateRecordStart": "2017-04-30T23:54:38.000-06:00",
"dateRecordEnd": "2017-04-30T23:59:10.000-06:00",
"dateDeleted": "2017-08-28T23:54:38.000-06:00",
"evidenceType": "Video",
"flag": "N",
"contentType": "mp4",
"sizeMb": 40.36958312988281,
"durationSeconds": "272.76",
"ownerFirstName": "D B",
"ownerLastName": "ITE",
"ownerBadgeId": "9999",
"ownerRole": "Officer/PSA's",
"ownerGroups": [],
"updatedByFirstName": "",
"updatedByLastName": "",
"updatedByBadgeId": "",
"updatedByRole": "",
"deletedByFirstName": "",
"deletedByLastName": "",
"deletedByBadgeId": "",
"deletedByRole": "",
"uploadedByFirstName": "D B",
"uploadedByLastName": "ITE",
"uploadedByBadgeId": "9999",
"uploadedByRole": "Officer/PSA's",
"gps": {
"latitude": null,
"longitude": null
},
"deviceId": "8888A888-8D88-4411-8819-DD55D9DDD66E",
"notes": [],
"categories": [
"120 Day Deletion"
],
"tags": [],
"cases": [],
"viewCount": 0,
"lastViewedOn": null,
"isReassigned": false,
"authenticatedShareCount": 0,
"deletionType": "",
"checksum": "{sha2}33aa3724c8b3bb647a27fb3a895867c8214508b0cdba6e66882095f22b29f23d",
"downloadCount": 0
},
{
"evidenceId": "7777A777-9E99-4411-8819-EE55E9EEE55E",
"status": "Active",
"title": "Video 2017-04-30 2354",
"idExternal": null,
"description": null,
"dateUploaded": "2017-05-02T01:02:08.180-06:00",
"dateModified": "2017-05-02T01:02:08.530-06:00",
"dateRecordStart": "2017-04-30T23:54:04.000-06:00",
"dateRecordEnd": "2017-05-01T00:01:53.000-06:00",
"dateDeleted": "2017-08-28T23:54:04.000-06:00",
"evidenceType": "Video",
"flag": "N",
"contentType": "mp4",
"sizeMb": 68.5533447265625,
"durationSeconds": "469.03",
"ownerFirstName": "C S",
"ownerLastName": "NARRO",
"ownerBadgeId": "5555",
"ownerRole": "Officer/PSA's",
"ownerGroups": [],
"updatedByFirstName": "",
"updatedByLastName": "",
"updatedByBadgeId": "",
"updatedByRole": "",
"deletedByFirstName": "",
"deletedByLastName": "",
"deletedByBadgeId": "",
"deletedByRole": "",
"uploadedByFirstName": "C S",
"uploadedByLastName": "NARRO",
"uploadedByBadgeId": "5555",
"uploadedByRole": "Officer/PSA's",
"gps": {
"latitude": null,
"longitude": null
},
"deviceId": "2F87080C-8AB2-4BB5-A3F4-E8E51D648B79",
"notes": [],
"categories": [
"120 Day Deletion"
],
"tags": [],
"cases": [],
"viewCount": 0,
"lastViewedOn": null,
"isReassigned": false,
"authenticatedShareCount": 0,
"deletionType": "",
"checksum": "{sha2}47256fe19712a852198a5ac45aef949f4b1ad1011dddc16b6674add514a2d614",
"downloadCount": 0
}
],
}
Edit - 5/5 Additional Information:
Here is my updated code for my actual project. I can now see that I am getting data if I restrict the parameters to a single result. but when I increase the results I can't get the foreach to work correctly. I have had to hardcode the array values. I appreciate any help in getting this working!!
#region Class
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
#region Methods
/// <summary>Outputs records to the output buffer</summary>
public override void CreateNewOutputRows()
{
//Set Webservice URL
string wUrl = "https://api.evidence.com/api/v1/agencies/{partnerId}/reports/data?reportType=EvidenceCreated&fromDate=2017-04-01&toDate=2017-04-30&pageSize=5&pageOffset=0";
try
{
//Call getWebServiceResult to return our Article attributes
RootObject outPutResponse = GetWebServiceResult(wUrl);
//If we get data back
if (outPutResponse != null)
{
foreach (Datum ar in outPutResponse.data)
// {
//Output main attributes of Article
EvidenceBuffer.AddRow();
EvidenceBuffer.evidenceId = outPutResponse.data[0].evidenceId;
EvidenceBuffer.status = outPutResponse.data[0].status;
EvidenceBuffer.title = outPutResponse.data[0].title;
EvidenceBuffer.idExternal = outPutResponse.data[0].idExternal;
EvidenceBuffer.description = outPutResponse.data[0].description;
EvidenceBuffer.dateUploaded = outPutResponse.data[0].dateUploaded;
EvidenceBuffer.dateModified = outPutResponse.data[0].dateModified;
EvidenceBuffer.dateRecordStart = outPutResponse.data[0].dateRecordStart;
EvidenceBuffer.dateRecordEnd = outPutResponse.data[0].dateRecordEnd;
EvidenceBuffer.dateDeleted = outPutResponse.data[0].dateDeleted;
EvidenceBuffer.evidenceType = outPutResponse.data[0].evidenceType;
EvidenceBuffer.flag = outPutResponse.data[0].flag;
EvidenceBuffer.contentType = outPutResponse.data[0].contentType;
EvidenceBuffer.sizeMb = outPutResponse.data[0].sizeMb;
EvidenceBuffer.durationSeconds = outPutResponse.data[0].durationSeconds;
EvidenceBuffer.ownerFirstName = outPutResponse.data[0].ownerFirstName;
EvidenceBuffer.ownerLastName = outPutResponse.data[0].ownerLastName;
EvidenceBuffer.ownerBadgeId = outPutResponse.data[0].ownerBadgeId;
EvidenceBuffer.ownerRole = outPutResponse.data[0].ownerRole;
EvidenceBuffer.ownerGroups = outPutResponse.data[0].ownerGroups;
EvidenceBuffer.updatedByFirstName = outPutResponse.data[0].updatedByFirstName;
EvidenceBuffer.updatedByLastName = outPutResponse.data[0].updatedByLastName;
EvidenceBuffer.updatedByBadgeId = outPutResponse.data[0].updatedByBadgeId;
EvidenceBuffer.updatedByRole = outPutResponse.data[0].updatedByRole;
EvidenceBuffer.deletedByFirstName = outPutResponse.data[0].deletedByFirstName;
EvidenceBuffer.deletedByLastName = outPutResponse.data[0].deletedByLastName;
EvidenceBuffer.deletedByBadgeId = outPutResponse.data[0].deletedByBadgeId;
EvidenceBuffer.deletedByRole = outPutResponse.data[0].deletedByRole;
EvidenceBuffer.uploadedByFirstName = outPutResponse.data[0].uploadedByFirstName;
EvidenceBuffer.uploadedByLastName = outPutResponse.data[0].uploadedByLastName;
EvidenceBuffer.uploadedByBadgeId = outPutResponse.data[0].uploadedByBadgeId;
EvidenceBuffer.uploadedByRole = outPutResponse.data[0].uploadedByRole;
EvidenceBuffer.gpslat = null;
EvidenceBuffer.gpslong = null;
EvidenceBuffer.deviceId = outPutResponse.data[0].deviceId;
EvidenceBuffer.notes = outPutResponse.data[0].notes[0];
EvidenceBuffer.categories = outPutResponse.data[0].categories[0];
EvidenceBuffer.tags = outPutResponse.data[0].tags[0];
EvidenceBuffer.cases = outPutResponse.data[0].cases[0];
EvidenceBuffer.viewCount = outPutResponse.data[0].viewCount;
EvidenceBuffer.lastViewedOn = outPutResponse.data[0].lastViewedOn;
EvidenceBuffer.isReassigned = outPutResponse.data[0].isReassigned;
EvidenceBuffer.authenicatedShareCount = outPutResponse.data[0].authenticatedShareCount;
EvidenceBuffer.deletionType = outPutResponse.data[0].deletionType;
EvidenceBuffer.checksum = outPutResponse.data[0].checksum;
EvidenceBuffer.downloadCount = outPutResponse.data[0].downloadCount;
//}
}
}
catch (Exception e)
{
FailComponent(e.ToString());
}
}
/// <summary>
/// Method to return our list articles
/// </summary>
/// <param name="wUrl">The web service URL to call</param>
/// <returns>An object that contains a list of Articles</returns>
private RootObject GetWebServiceResult(string wUrl)
{
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl);
httpWReq.Headers.Add("Authorization", "Bearer SecretCode");
httpWReq.Method = "GET";
httpWReq.ContentType = "application/json";
HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse();
RootObject jsonResponse = null;
try
{
//Get the stream of JSON
Stream responseStream = httpWResp.GetResponseStream();
//Deserialize the JSON stream
using (StreamReader reader = new StreamReader(responseStream))
{
//Deserialize our JSON
DataContractJsonSerializer sr = new DataContractJsonSerializer(typeof(RootObject));
jsonResponse = (RootObject)sr.ReadObject(responseStream);
}
}
//Output JSON parsing error
catch (Exception e)
{
FailComponent(e.ToString());
}
return jsonResponse;
}
/// <summary>
/// Outputs error message
/// </summary>
/// <param name="errorMsg">Full error text</param>
private void FailComponent(string errorMsg)
{
bool fail = false;
IDTSComponentMetaData100 compMetadata = this.ComponentMetaData;
compMetadata.FireError(1, "Error Getting Data From Webservice!", errorMsg, "", 0, out fail);
}
#endregion
}
#endregion
#region JSON Classes
//Class to hold attributes of the Article
[DataContract]
public class Gps
{
[DataMember(Name = "latitude")]
public object latitude { get; set; }
[DataMember(Name = "longitude")]
public object longitude { get; set; }
}
public class Datum
{
[DataMember(Name = "evidenceId")]
public string evidenceId { get; set; }
[DataMember(Name = "status")]
public string status { get; set; }
[DataMember(Name = "title")]
public string title { get; set; }
[DataMember(Name = "idExternal")]
public string idExternal { get; set; }
[DataMember(Name = "description")]
public string description { get; set; }
[DataMember(Name = "dateUploaded")]
public string dateUploaded { get; set; }
[DataMember(Name = "dateModified")]
public string dateModified { get; set; }
[DataMember(Name = "dateRecordStart")]
public string dateRecordStart { get; set; }
[DataMember(Name = "dateRecordEnd")]
public string dateRecordEnd { get; set; }
[DataMember(Name = "dateDeleted")]
public string dateDeleted { get; set; }
[DataMember(Name = "evidenceType")]
public string evidenceType { get; set; }
[DataMember(Name = "flag")]
public string flag { get; set; }
[DataMember(Name = "contentType")]
public string contentType { get; set; }
[DataMember(Name = "sizeMb")]
public float sizeMb { get; set; }
[DataMember(Name = "durationSeconds")]
public float durationSeconds { get; set; }
[DataMember(Name = "ownerFirstName")]
public string ownerFirstName { get; set; }
[DataMember(Name = "ownerLastName")]
public string ownerLastName { get; set; }
[DataMember(Name = "ownerBadgeId")]
public string ownerBadgeId { get; set; }
[DataMember(Name = "ownerRole")]
public string ownerRole { get; set; }
[DataMember(Name = "ownerGroups")]
public string ownerGroups { get; set; }
[DataMember(Name = "updatedByFirstName")]
public string updatedByFirstName { get; set; }
[DataMember(Name = "updatedByLastName")]
public string updatedByLastName { get; set; }
[DataMember(Name = "updatedByBadgeId")]
public string updatedByBadgeId { get; set; }
[DataMember(Name = "updatedByRole")]
public string updatedByRole { get; set; }
[DataMember(Name = "deletedByFirstName")]
public string deletedByFirstName { get; set; }
[DataMember(Name = "deletedByLastName")]
public string deletedByLastName { get; set; }
[DataMember(Name = "deletedByBadgeId")]
public string deletedByBadgeId { get; set; }
[DataMember(Name = "deletedByRole")]
public string deletedByRole { get; set; }
[DataMember(Name = "uploadedByFirstName")]
public string uploadedByFirstName { get; set; }
[DataMember(Name = "uploadedByLastName")]
public string uploadedByLastName { get; set; }
[DataMember(Name = "uploadedByBadgeId")]
public string uploadedByBadgeId { get; set; }
[DataMember(Name = "uploadedByRole")]
public string uploadedByRole { get; set; }
[DataMember(Name = "gps")]
public Gps gps { get; set; }
[DataMember(Name = "deviceId")]
public string deviceId { get; set; }
[DataMember(Name = "notes")]
public List<string> notes { get; set; }
[DataMember(Name = "categories")]
public List<string> categories { get; set; }
[DataMember(Name = "tags")]
public List<string> tags { get; set; }
[DataMember(Name = "cases")]
public List<string> cases { get; set; }
[DataMember(Name = "viewCount")]
public int viewCount { get; set; }
[DataMember(Name = "lastViewedOn")]
public string lastViewedOn { get; set; }
[DataMember(Name = "isReassigned")]
public bool isReassigned { get; set; }
[DataMember(Name = "authenticatedShareCount")]
public int authenticatedShareCount { get; set; }
[DataMember(Name = "deletionType")]
public string deletionType { get; set; }
[DataMember(Name = "checksum")]
public string checksum { get; set; }
[DataMember(Name = "downloadCount")]
public int downloadCount { get; set; }
}
//Root object that contains a List of data
[DataContract]
public class RootObject
{
[DataMember(Name = "data")]
public List<Datum> data { get; set; }
}
#endregion
Original post:
I am using this sample to try and get an SSIS package to consume from an API. I was unable to get my JSON to work (and the API used in the sample has moved so I was unable to get it to work "as is") so I decided to try with something easy.
https://dennysjymbo.blogspot.com/2014/03/utilizing-net-40-datacontractjsonserial.html?showComment=1493916532059#c208608008820233205
I am trying to create a simple SSIS package to consume this information from https://swapi.co/api/people/1
Here is my script:
#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Net;
using Microsoft.SqlServer.Dts.Runtime;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using System.Collections.Generic;
using System.Text;
#endregion
#region Class
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
#region Methods
/// <summary>Outputs records to the output buffer</summary>
public override void CreateNewOutputRows()
{
//Set Webservice URL
//string wUrl = "https://api.evidence.com/api/v1/agencies/3DCB15D3-3770-4DC4-8D22-4FB1FA8619A5/reports/data?reportType=EvidenceCreated&fromDate=2017-04-01&toDate=2017-04-30&pageSize=5&pageOffset=0";
string wUrl = "http://swapi.co/api/people/1";
try
{
//Call getWebServiceResult to return our Article attributes
RootObject outPutResponse = GetWebServiceResult(wUrl);
//If we get data back
if (outPutResponse != null)
{
foreach (People ar in outPutResponse.people)
{
//Output main attributes of Article
PeopleBuffer.AddRow();
PeopleBuffer.name = ar.name;
PeopleBuffer.height = ar.height;
PeopleBuffer.mass = ar.mass;
PeopleBuffer.haircolor = ar.hair_color;
PeopleBuffer.skincolor = ar.skin_color;
PeopleBuffer.eyecolor = ar.eye_color;
PeopleBuffer.birthyear = ar.birth_year;
PeopleBuffer.gender = ar.gender;
PeopleBuffer.homeworld = ar.homeworld;
PeopleBuffer.films = ar.films[0];
PeopleBuffer.species = ar.species[0];
PeopleBuffer.vehicles = ar.vehicles[0];
PeopleBuffer.starships = ar.starships[0];
PeopleBuffer.created = ar.created;
PeopleBuffer.edited = ar.edited;
PeopleBuffer.url = ar.url;
}
}
}
catch (Exception e)
{
FailComponent(e.ToString());
}
}
/// <summary>
/// Method to return our list articles
/// </summary>
/// <param name="wUrl">The web service URL to call</param>
/// <returns>An object that contains a list of Articles</returns>
private RootObject GetWebServiceResult(string wUrl)
{
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl);
//httpWReq.Headers.Add("Authorization", "Bearer HxS35IIN36b9EW64L+GG3xYhoz66bNaD8hsckfQGPdk=");
httpWReq.Method = "GET";
httpWReq.ContentType = "application/json";
HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse();
RootObject jsonResponse = null;
try
{
//Get the stream of JSON
Stream responseStream = httpWResp.GetResponseStream();
//Deserialize the JSON stream
using (StreamReader reader = new StreamReader(responseStream))
{
//Deserialize our JSON
DataContractJsonSerializer sr = new DataContractJsonSerializer(typeof(RootObject));
jsonResponse = (RootObject)sr.ReadObject(responseStream);
}
}
//Output JSON parsing error
catch (Exception e)
{
FailComponent(e.ToString());
}
return jsonResponse;
}
/// <summary>
/// Outputs error message
/// </summary>
/// <param name="errorMsg">Full error text</param>
private void FailComponent(string errorMsg)
{
bool fail = false;
IDTSComponentMetaData100 compMetadata = this.ComponentMetaData;
compMetadata.FireError(1, "Error Getting Data From Webservice!", errorMsg, "", 0, out fail);
}
#endregion
}
#endregion
#region JSON Classes
//Class to hold attributes of the Article
[DataContract]
public class People
{
[DataMember(Name = "name")]
public string name { get; set; }
[DataMember(Name = "height")]
public string height { get; set; }
[DataMember(Name = "mass")]
public string mass { get; set; }
[DataMember(Name = "hair_color")]
public string hair_color { get; set; }
[DataMember(Name = "skin_color")]
public string skin_color { get; set; }
[DataMember(Name = "eye_color")]
public string eye_color { get; set; }
[DataMember(Name = "birth_year")]
public string birth_year { get; set; }
[DataMember(Name = "gender")]
public string gender { get; set; }
[DataMember(Name = "homeworld")]
public string homeworld { get; set; }
[DataMember(Name = "films")]
public List<string> films { get; set; }
[DataMember(Name = "species")]
public List<string> species { get; set; }
[DataMember(Name = "vehicles")]
public List<string> vehicles { get; set; }
[DataMember(Name = "starships")]
public List<string> starships { get; set; }
[DataMember(Name = "created")]
public DateTime created { get; set; }
[DataMember(Name = "edited")]
public DateTime edited { get; set; }
[DataMember(Name = "url")]
public string url { get; set; }
}
//Root object that contains a List of Articles
[DataContract]
public class RootObject
{
[DataMember(Name = "people")]
public List<People> people { get; set; }
}
#endregion
this is the line that is not working correctly:
jsonResponse = (RootObject)sr.ReadObject(responseStream);
In the image you can see that it added a people object but none of the data is there.
I would appreciate any help in figuring out what I need to modify in order to get the data in the response object.
Thanks!
Edit:
The actual error message that the ssis package gives is:
Error: 0x1 at Data Flow Task, Error Getting Data From Webservice!: System.NullReferenceException: Object reference not set to an instance of an object.
at ScriptMain.CreateNewOutputRows() in c:\Users\E37026\AppData\Local\Temp\Vsta\747a4fee6a1f4610a91610400e06a1ac\main.cs:line 81
Couple things here. First your url should be http://swapi.co/api/people/1/?format=json. Second is that you are not getting an array of people back, so you need to change this to a single object. Third the serializer doesn't like the datetime values, I changed them to strings and it worked. You can work on changing this in the staging area or in a derived column transform. Here is the code:
#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Net;
using Microsoft.SqlServer.Dts.Runtime;
using System.IO;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
using System.Collections.Generic;
using System.Text;
#endregion
#region Class
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
#region Methods
/// <summary>Outputs records to the output buffer</summary>
public override void CreateNewOutputRows()
{
//Set Webservice URL
//string wUrl = "https://api.evidence.com/api/v1/agencies/3DCB15D3-3770-4DC4-8D22-4FB1FA8619A5/reports/data?reportType=EvidenceCreated&fromDate=2017-04-01&toDate=2017-04-30&pageSize=5&pageOffset=0";
string wUrl = "http://swapi.co/api/people/1/?format=json";
try
{
//Call getWebServiceResult to return our Article attributes
People outPutResponse = GetWebServiceResult(wUrl);
//If we get data back
if (outPutResponse != null)
{
//Output main attributes of Article
PeopleBuffer.AddRow();
PeopleBuffer.name = outPutResponse.name;
PeopleBuffer.height = outPutResponse.height;
PeopleBuffer.mass = outPutResponse.mass;
PeopleBuffer.haircolor = outPutResponse.hair_color;
PeopleBuffer.skincolor = outPutResponse.skin_color;
PeopleBuffer.eyecolor = outPutResponse.eye_color;
PeopleBuffer.birthyear = outPutResponse.birth_year;
PeopleBuffer.gender = outPutResponse.gender;
PeopleBuffer.homeworld = outPutResponse.homeworld;
PeopleBuffer.films = outPutResponse.films[0];
PeopleBuffer.species = outPutResponse.species[0];
PeopleBuffer.vehicles = outPutResponse.vehicles[0];
PeopleBuffer.starships = outPutResponse.starships[0];
PeopleBuffer.created = outPutResponse.created;
PeopleBuffer.edited = outPutResponse.edited;
PeopleBuffer.url = outPutResponse.url;
}
}
catch (Exception e)
{
FailComponent(e.ToString());
}
}
/// <summary>
/// Method to return our list articles
/// </summary>
/// <param name="wUrl">The web service URL to call</param>
/// <returns>An object that contains a list of Articles</returns>
private People GetWebServiceResult(string wUrl)
{
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create(wUrl);
//httpWReq.Headers.Add("Authorization", "Bearer HxS35IIN36b9EW64L+GG3xYhoz66bNaD8hsckfQGPdk=");
httpWReq.Method = "GET";
httpWReq.ContentType = "application/json";
HttpWebResponse httpWResp = (HttpWebResponse)httpWReq.GetResponse();
People jsonResponse = null;
try
{
//Get the stream of JSON
Stream responseStream = httpWResp.GetResponseStream();
//Deserialize the JSON stream
using (StreamReader reader = new StreamReader(responseStream))
{
//Deserialize our JSON
DataContractJsonSerializer sr = new DataContractJsonSerializer(typeof(People));
jsonResponse = (People)sr.ReadObject(responseStream);
}
}
//Output JSON parsing error
catch (Exception e)
{
FailComponent(e.ToString());
}
return jsonResponse;
}
/// <summary>
/// Outputs error message
/// </summary>
/// <param name="errorMsg">Full error text</param>
private void FailComponent(string errorMsg)
{
bool fail = false;
IDTSComponentMetaData100 compMetadata = this.ComponentMetaData;
compMetadata.FireError(1, "Error Getting Data From Webservice!", errorMsg, "", 0, out fail);
}
#endregion
}
#endregion
#region JSON Classes
//Class to hold attributes of the Article
[DataContract]
public class People
{
[DataMember(Name = "name")]
public string name { get; set; }
[DataMember(Name = "height")]
public string height { get; set; }
[DataMember(Name = "mass")]
public string mass { get; set; }
[DataMember(Name = "hair_color")]
public string hair_color { get; set; }
[DataMember(Name = "skin_color")]
public string skin_color { get; set; }
[DataMember(Name = "eye_color")]
public string eye_color { get; set; }
[DataMember(Name = "birth_year")]
public string birth_year { get; set; }
[DataMember(Name = "gender")]
public string gender { get; set; }
[DataMember(Name = "homeworld")]
public string homeworld { get; set; }
[DataMember(Name = "films")]
public List<string> films { get; set; }
[DataMember(Name = "species")]
public List<string> species { get; set; }
[DataMember(Name = "vehicles")]
public List<string> vehicles { get; set; }
[DataMember(Name = "starships")]
public List<string> starships { get; set; }
[DataMember(Name = "created")]
public string created { get; set; }
[DataMember(Name = "edited")]
public string edited { get; set; }
[DataMember(Name = "url")]
public string url { get; set; }
}
#endregion

Deserialize JSon result with array

I have some problem to read from a json. Here is the json what I get:
{
"LoginName": null,
"EmailAddress": null,
"FirstName": null,
"LastName": null,
"ErrorList": [
{
"ErrorNumber": 5001,
"ErrorMessage": "Validation Error: The UserName field is required.",
"Details": null
},
{
"ErrorNumber": 5001,
"ErrorMessage": "Validation Error: The Password field is required.",
"Details": null
},
{
"ErrorNumber": 1,
"ErrorMessage": "The username or password is not correct!",
"Details": null
}
]
}
Here are my classes:
[DataContract]
public class User
{
[DataMember(Name = "LoginName")]
public string loginName { get; set; }
[DataMember(Name = "EmailAddress")]
public string emailAddress { get; set; }
[DataMember(Name = "LastName")]
public string lastName { get; set; }
[DataMember(Name = "FirstName")]
public string firstName { get; set; }
[DataMember(Name = "ErrorList")]
public ErrorList[] errorList { get; set; }
}
[DataContract]
public class ErrorList
{
[DataMember(Name = "ErrorNumber")]
public int errorNumber { get; set; }
[DataMember(Name = "ErrorMessage")]
public string errorMessage { get; set; }
}
I can get for example the LoginName and EmailAddress, but I can't get the element of ErrorList.
Here is the code for deserialize the json result:
var res = await result.Content.ReadAsStringAsync();
var answer = JsonConvert.DeserializeObject<User>(res);
Email.Text = answer.emailAddress;
It works, but I don't reach the array.
Anyone has any idea to solve this problem?
As #Abdurrahman Köken stated, maybe you should use JSON.NET attributes instead of System.Runtime.Serialization.
Still, this code has nothing wrong with it. Error is somewhere else. Using your User class as-is, with provided JSON, and deserializing it as var answer = JsonConvert.DeserializeObject<User>(yourJsonHere); gives the following:
So, after deserialization
answer.errorList.ToList().ForEach(error =>
Console.WriteLine(#"{0}={1}", error.errorNumber, error.errorMessage));
Outputs
5001=Validation Error: The UserName field is required.
5001=Validation Error: The Password field is required.
1=The username or password is not correct!
You should change DataMember attributes with JsonProperty. This should work:
public class User
{
[JsonProperty("LoginName")]
public string loginName { get; set; }
[JsonProperty("EmailAddress")]
public string emailAddress { get; set; }
[JsonProperty("LastName")]
public string lastName { get; set; }
[JsonProperty("FirstName")]
public string firstName { get; set; }
[JsonProperty("ErrorList")]
public ErrorList[] errorList { get; set; }
}
public class ErrorList
{
[JsonProperty("ErrorNumber")]
public int errorNumber { get; set; }
[JsonProperty("ErrorMessage")]
public string errorMessage { get; set; }
}

How to Deserialize Json String with the help of "DataContractJsonSerializer". My Json string has no any name as MainObject

I am unable to deserialize my Json String with the help of "DataContractJsonSerializer". I can't use third party tool for deserialization, my json string has no any enter code hereMainObject so i am unable to do, Please help me. My json data is below.
[{
"id": "2348",
"fo": "",
"na": "og",
"ex": "",
"ge": "",
"no_cl": "Phr",
"wo_cl": {
"id": "27",
"na": "kon",
"na_cl": "WordClass"
},
"con": []
}]
my classes according to above Json is following.
public class WoCl
{
public string id { get; set; }
public string na { get; set; }
public string na_cl { get; set; }
}
public class RootObject
{
public string id { get; set; }
public string fo { get; set; }
public string na { get; set; }
public string ex { get; set; }
public string ge { get; set; }
public string no_cl { get; set; }
public WoCl wo_cl { get; set; }
public List<object> con { get; set; }
}
My deserializing code is following.
string json = returnDictionaryJsonFromServer();//this function returning above json data
List<MainObject> obj = new List<MainObject>();
var memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(json));
var serializer = new DataContractJsonSerializer(obj.GetType());
obj = serializer.ReadObject(memoryStream) as List<MainObject>;
return obj;
public AllAnnotatedData Deserializer(string json)
{
MyWrapper obj = new MyWrapper();
using (var memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
try
{
var serializer = new DataContractJsonSerializer(obj.GetType());
obj = (MyWrapper)serializer.ReadObject(memoryStream);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
return obj;
}
//MyWrapper is following.
[DataContract]
public class MyWrapper
{
[DataMember(Name = "objects")]
public List<mySecondWrapper> mySecondWrapper //this is list of my wrapper class.
{ get; set; }
[DataMember(Name = "property1")]
public Meta AllAnnotationMeta
{ get; set; }
[DataMember(Name = "property2")]
public List<myThirdWrapper> myThirdWrapper //this is list of my wrapper class.
{ get; set; }
}