SSIS Script Component JSON DataContractJsonSerializer - json
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
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; } }
How make dropdownlist to access data on your SelectItemList using asp.net mvc?
I am using DropDownlist in order to get country of all the world. I have attached the file(country_list.txt) using srcFilePath. The error i am getting is "There is no ViewData item of type 'IEnumerable' that has the key 'SelectedCountryId'.What could be an issue, because my EditFormTrainingRegViewModel does have this field SelectedCountry as a primary key. Its been declared as public int? SelectedCountryId {get;set;} // List for countries. private IEnumerable<SelectListItem> GetCountryList() { SelectList listcn = null; try { var list = this.LoadData().Select(p => new SelectListItem { Value = p.Country_Id.ToString(), Text = p.Country_Name }); listcn = new SelectList(list, "Value", "Text"); }catch(Exception ex) { throw ex; } return listcn; } public ActionResult DropDownSelect() { EditTrainingRegFormViewModel model = new EditTrainingRegFormViewModel(); model.SelectedCountryId = 0; this.ViewBag.CountryList = this.GetCountryList(); return this. View(model); } // Loading data for country list. private List<EditTrainingRegFormViewModel> LoadData() { List<EditTrainingRegFormViewModel> lst = new List<EditTrainingRegFormViewModel>(); try { string line = string.Empty; string srcFilePath = "Content/files/country_list.txt"; var rootPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase); var fullPath = Path.Combine(rootPath, srcFilePath); string filePath = new Uri(fullPath).LocalPath; StreamReader src = new StreamReader(new FileStream(filePath, FileMode.Open, FileAccess.Read)); // while to read the file while((line = src.ReadLine()) !=null) { EditTrainingRegFormViewModel infoLst = new EditTrainingRegFormViewModel(); string[] info = line.Split(','); //Setting infoLst.Country_Id = Convert.ToInt32(info[0].ToString()); infoLst.Country_Name = info[1].ToString(); lst.Add(infoLst); } src.Dispose(); src.Close(); }catch(Exception ex) { Console.Write(ex); } return lst; } //View #Html.DropDownListFor(m=>m.SelectedCountryId, this.ViewBag.CountryList as SelectList, new {#class = "form-control"}) // Model class public class EditTrainingRegFormViewModel { public string Title { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Position { get; set; } public string Company { get; set; } public string Address { get; set; } [Display(Name = "Choose country")] public int ? SelectedCountryId { get; set; } public string Code { get; set; } public string City { get; set; } public string State { get; set; } public string Cell_Number { get; set; } public List<string> Dietary_requirement { get; set; } public string Email { get; set; } public int Country_Id { get; set; } public string Country_Name { get; set; } }
Deserialize multi-part JSON with DataContractJsonSerializer
I'm fairly new to the awesomeness that is JSON - I'm using the DataContractJsonSerializer. I cannot get the multiple instances of the Customer objects into the list. The Attributes work as expected but there are no Customer objects in my List..?? { "#attributes": {"count":"2", "offset":"0", "limit":"100" }, "Customer": { "firstName":"cust ", "lastName":"one", "title":"Owner", "company":"CustOne Plants", "companyRegistrationNumber":"", "vatNumber":"", "creditAccountID":"1", "customerTypeID":"4", "discountID":"0", "taxCategoryID":"0", "customerID":"1", "createTime":"2017-06-19T23:36:11+00:00", "timeStamp":"2017-06-20T18:55:11+00:00", "archived":"false" } "Customer": { "firstName":"cust ", "lastName":"two", "title":"Owner", "company":"CustTwo House of Games", "companyRegistrationNumber":"", "vatNumber":"", "creditAccountID":"1", "customerTypeID":"4", "discountID":"0", "taxCategoryID":"0", "customerID":"1", "createTime":"2017-06-19T23:36:11+00:00", "timeStamp":"2017-06-20T18:55:11+00:00", "archived":"false" } } .NET code: StreamReader stream = new StreamReader(#"C:\TMC Projects\PotteryManufacturing\CustomerJSON.txt"); string text = stream.ReadToEnd(); stream.Close(); byte[] byteArray = Encoding.UTF8.GetBytes(text); MemoryStream stream1 = new MemoryStream(byteArray); DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(CustomersRoot)); var varInfo = serializer.ReadObject(stream1) as CustomersRoot; stream1.Close(); and finally my classes/data contracts/data members: [DataContract] public class CustomersRoot { private List<Customer> m_Customers; public CustomersRoot() { this.Customer = new List<Customer>(); } [DataMember(Name ="#attributes")] public Attributes attrs { get; set; } [DataMember(Name = "Customer")] public List<Customer> Customer { get { return m_Customers; } set { m_Customers = value; } } } [DataContract] public class Customer { [DataMember(Name ="firstName")] public string firstName { get; set; } [DataMember(Name = "lastName")] public string lastName { get; set; } [DataMember(Name = "title")] public string title { get; set; } [DataMember(Name = "company")] public string company { get; set; } [DataMember(Name = "companyRegistrationNumber")] public string companyRegistrationNumber { get; set; } [DataMember(Name = "vatNumber")] public string vatNumber { get; set; } [DataMember(Name = "creditAccountID")] public int creditAccountID { get; set; } [DataMember(Name = "customerTypeID")] public int customerTypeID { get; set; } [DataMember(Name = "discountID")] public int discountID { get; set; } [DataMember(Name = "taxCategoryID")] public int taxCategoryID { get; set; } [DataMember(Name = "customerID")] public int customerID { get; set; } [DataMember(Name = "createTime")] public string createTime { get; set; } [DataMember(Name = "timeStamp")] public string timeStamp { get; set; } [DataMember(Name = "archived")] public bool archived { get; set; } } [DataContract] public class Attributes { [DataMember(Name = "count")] public int count { get; set; } [DataMember(Name = "offset")] public int offset { get; set; } [DataMember(Name = "limit")] public int limit { get; set; } }
I figured out what's going on here - the call can sometimes return an array of Customer objects (not formatted correctly above) OR a single instance of the object. When the web service returns a single Customer instance, the List code does not work. I will have to check on how to deal w/ this issue.
Parsing Json gMaps Api in windows Phone 8 using Serialization
I Have json file : Google Maps Api - place Json I want Parsing Json using System.Runtime.Serialization; I can create 3 class : public class Places { [DataMember(Name = "geometry")] public string Geometry { get; set; } [DataMember(Name = "location")] public string Location { get; set; } [DataMember(Name = "lat")] public string Latitude { get; set; } [DataMember(Name = "lng")] public string Longitude { get; set; } [DataMember(Name = "icon")] public string Icon { get; set; } [DataMember(Name = "id")] public string Id { get; set; } [DataMember(Name = "name")] public string Name { get; set; } [DataMember(Name = "photo")] public string Photo { get; set; } [DataMember(Name = "rating")] public string Rating { get; set; } [DataMember(Name = "reference")] public string Reference { get; set; } [DataMember(Name = "types")] public string Types { get; set; } [DataMember(Name = "vicinity")] public string Vicinity { get; set; } } public class AppConstants { public static String baseUri = "https://maps.googleapis.com/maps/api/place/search/json?location="; } public class PlaceToMap { public GeoCoordinate Coordinate { get; set; } public string Info { get; set; } } [DataContract] public class PlacesList { [DataMember(Name ="results")] public List<Places> PlaceList { get; set; } } MainPage.xaml.cs : private void updateMap(PlacesList googlePlaceApiRespone) { int totalRecords = googlePlaceApiRespone.PlaceList.Count(); try { ObservableCollection<PlaceToMap> placeToMapObjs = new ObservableCollection<PlaceToMap>(); for (int index = 0; index < totalRecords; index++) { placeToMapObjs.Add(new PlaceToMap() { Coordinate = new GeoCoordinate(Convert.ToDouble(googlePlaceApiRespone.PlaceList.ElementAt(index).Latitude), Convert.ToDouble(googlePlaceApiRespone.PlaceList.ElementAt(index).Longitude)), Info = googlePlaceApiRespone.PlaceList.ElementAt(index).Name + Environment.NewLine + googlePlaceApiRespone.PlaceList.ElementAt(index).Vicinity }); } ObservableCollection<DependencyObject> children = MapExtensions.GetChildren(myMap); var obj = children.FirstOrDefault(x => x.GetType() == typeof(MapItemsControl)) as MapItemsControl; obj.ItemsSource = placeToMapObjs; myMap.SetView(new GeoCoordinate(Convert.ToDouble(currentLatitude), Convert.ToDouble(currentLongitude)), 16); } catch (Exception) { } } But can't no show in pushpin. toolkit:Pushpin GeoCoordinate="{Binding Coordinate}" Content="{Binding Info}"
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; } }