Windows Phone, How to deserialize json which has nested Array - json

I am now doing a windows phone project, and need to request to a web service for some json data.
if the json structure is like
[Dictionary1,
Dictionary2,
Dictionary3
]
then, DataContractJsonSerializer works fine.
but the next request i will get a complex json data, it doesn't work. like
[
[Dictionary1],
[Dictionary2],
[Dictionary3]
]
real data is:
[
[{"length":5734.042,"duration":1680,"legs":
[
{"length":685,"duration":555.42,"type":"walk","locs":
[
{"coord":{"x":2546445,"y":6675512},"arrTime":"201203290959","depTime":"201203290959","name":null},
{"coord":{"x":2546433.8,"y":6675498.3},"arrTime":"201203291000","depTime":"201203291000","name":"Teekkaripolku"}
]
},
{"length":4158,"duration":420,"type":"5","code":"2506 2","locs":
[
{"coord":{"x":2546168,"y":6674959},"arrTime":"201203291009","depTime":"201203291009","name":"Alvar Aallon puisto","code":"2222235","shortCode":"E2226","stopAddress":"Otaniementie"},
{"coord":{"x":2546337,"y":6674857},"arrTime":"201203291009","depTime":"201203291009","name":"Dipoli","code":"2222204","shortCode":"E2204","stopAddress":"Otaniementie"}
]
}
]
}],
[{"length":5734.042,"duration":1680,"legs":
[
{"length":685,"duration":555.42,"type":"1", "code":"1111", "locs":
[
{"coord":{"x":2546445,"y":6675512},"arrTime":"201203290952","depTime":"201203290952","name":null},
{"coord":{"x":2546433.8,"y":6675498.3},"arrTime":"201203290953","depTime":"201203290953","name":"Teekkaripolku"}
]
},
{"length":4158,"duration":420,"type":"5","code":"2194 2","locs":
[ {"coord":{"x":2546168,"y":6674959},"arrTime":"201203291002","depTime":"201203291002","name":"Alvar Aallon puisto","code":"2222235","shortCode":"E2226","stopAddress":"Otaniementie"},
{"coord":{"x":2546337,"y":6674857},"arrTime":"201203291002","depTime":"201203291002","name":"Dipoli","code":"2222204","shortCode":"E2204","stopAddress":"Otaniementie"}
]
}
]
}]
]
and the class models are :
[DataContract]
public class RouteList
{
[DataMember]
public List<Route> oneRoute;
}
---
[DataContract]
public class Route
{
[DataMember(Name = "length", IsRequired = true)]
public Double length { get; set; }
[DataMember(Name = "duration", IsRequired = true)]
public Double duration { get; set; }
[DataMember(Name = "legs", IsRequired = true)]
public List<Leg> legs { get; set; }
}
----
[DataContract]
public class Leg
{
[DataMember(Name = "length", IsRequired = true)]
public Double length { get; set; }
[DataMember(Name = "duration", IsRequired = true)]
public Double duration { get; set; }
[DataMember(Name = "type", IsRequired = true)]
public String type { get; set; }
[DataMember(Name = "code", IsRequired = false)]
public String code { get; set; }
[DataMember(Name = "locs", IsRequired = true)]
public List<Loc> locs { get; set; }
[DataMember(Name = "shape", IsRequired = false)]
public String shape { get; set; }
}
-----
[DataContract]
public class Loc
{
[DataMember(Name = "coord", IsRequired = true)]
public String coord { get; set; }
[DataMember(Name = "arrTime", IsRequired = true)]
public String arrTime { get; set; }
[DataMember(Name = "depTime", IsRequired = true)]
public String depTime { get; set; }
[DataMember(Name = "name", IsRequired = true)]
public String name { get; set; }
[DataMember(Name = "code", IsRequired = false)]
public String code { get; set; }
[DataMember(Name = "shortCode", IsRequired = false)]
public String shortCode { get; set; }
[DataMember(Name = "stopAddress", IsRequired = false)]
public String stopAddress { get; set; }
}
and the deserializing code:
System.IO.MemoryStream mStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(e.Result));
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(List<RouteList>));
result = (List<RouteList>)serializer.ReadObject(mStream);
i have tried DataContractJsonSerializer and json.NET. but failed. and the most possible problem is the nested arrays, the model:RouteList. anyone knows how to fix it? Thanks a lot.

The issue may be because of your class creation, Before concluding check you model class that handle you parsed result. Or if you want help add more information, like the structure of your model class, the code you used for parsing json etc. Here it seems nothing is complected. Json.Net will do all the tricks, the only effort you need to take is proper building of you class type ..
Enjoy Coding

with the help of Olli Saarikivi, the problem is solved.
with his permission, post his solution here, in case anybody else may need it.
// The request response handler
var routeResults = new List<CompoundRoute>();
if (e.Result.Trim().Length != 0)
{
JArray json = JArray.Parse(e.Result);
foreach (var token in json)
{
var routeArray = token as JArray;
if (routeArray != null)
{
var compoundRoute = new CompoundRoute { Routes = new Route[routeArray.Count] };
for (int i = 0; i < compoundRoute.Routes.Length; ++i)
{
compoundRoute.Routes[i] = new Route(routeArray[i]);
}
routeResults.Add(compoundRoute);
}
}
}
// The data model
[DataContract]
public class CompoundRoute
{
private static readonly Route[] EmptyRoutes = new Route[0];
public CompoundRoute()
{
Routes = EmptyRoutes;
}
[DataMember]
public Route[] Routes;
}
[DataContract]
public class Route
{
public Route(JToken token)
{
Length = token.Value<double>("length");
int durationSeconds = token.Value<int>("duration");
Duration = TimeSpan.FromSeconds(durationSeconds);
JArray legTokens = token["legs"] as JArray;
if (legTokens != null)
{
Legs = new Leg[legTokens.Count];
for (int i = 0; i < Legs.Length; ++i)
{
Legs[i] = new Leg(legTokens[i]);
}
}
}
[DataMember]
public double Length;
[DataMember]
public TimeSpan Duration;
[DataMember]
public Leg[] Legs;
}
[DataContract]
public class Leg
{
public Leg(JToken token)
{
Length = token.Value<double>("length");
double durationSeconds = token.Value<double>("duration");
Duration = TimeSpan.FromSeconds((int)Math.Round(durationSeconds));
Type = token.Value<string>("type");
string lineCode = token.Value<string>("code");
if (lineCode != null)
{
Line = App.Cache.GetOrCreate(lineCode, () => new Line(lineCode));
}
if (Type == "12") // Commuter trains
{
Line.ShortName = Utils.RemoveLeadingNumbers(Line.ShortName);
}
JArray locTokens = token["locs"] as JArray;
if (locTokens != null)
{
Locs = new LegLocation[locTokens.Count];
for (int i = 0; i < Locs.Length; ++i)
{
Locs[i] = new LegLocation(locTokens[i]);
}
}
else
{
Locs = new LegLocation[0];
}
JArray shapeTokens = token["shape"] as JArray;
if (shapeTokens != null)
{
Shape = new ReittiCoordinate[shapeTokens.Count];
for (int i = 0; i < Shape.Length; ++i)
{
var shapeToken = shapeTokens[i];
double x = shapeToken.Value<double>("x");
double y = shapeToken.Value<double>("y");
var coordinate = new ReittiCoordinate(y, x);
Shape[i] = coordinate;
}
}
}
[DataMember]
public double Length;
[DataMember]
public TimeSpan Duration;
[DataMember]
public string Type;
[DataMember]
public Line Line;
[DataMember]
public LegLocation[] Locs;
[DataMember]
public ReittiCoordinate[] Shape;
}
[DataContract]
public class LegLocation
{
public LegLocation(JToken token)
{
var coordToken = token["coord"];
double x = coordToken.Value<double>("x");
double y = coordToken.Value<double>("y");
Coord = new ReittiCoordinate(y, x);
string arrTimeString = token.Value<string>("arrTime");
ArrTime = DateTime.ParseExact(arrTimeString, "yyyyMMddHHmm", null);
string depTimeString = token.Value<string>("depTime");
DepTime = DateTime.ParseExact(depTimeString, "yyyyMMddHHmm", null);
Name = token.Value<string>("name");
}
[DataMember]
public ReittiCoordinate Coord;
[DataMember]
public DateTime ArrTime;
[DataMember]
public DateTime DepTime;
[DataMember]
public string Name;
}

Related

JsonConvert.DeseDeserializeObject return null c# in ASP.NET MVC

I have an API request in my web application but every time I convert the response result to deserialize object it gives null value to my model.
Here's my code:
var contents = await responseMessage.Content.ReadAsStringAsync();
var statusInfo = responseMessage.StatusCode.ToString();
if (statusInfo == "OK")
{
var jsonresult = JObject.Parse(contents);
var respond = jsonresult["data"].ToString();
var result = JsonConvert.DeserializeObject<ResponseModel>(respond);
}
The contents value is
"{\"data\":{\"totalcount:\":8113,\"tpa:\":6107,\"tip:\":5705},\"message\":\"success\"}"
The respond value is
"{ \r\n"totalcount:\": 8113,\r\n \"tpa:\": 6107,\r\n \"tip:\": 5705\r\n}"
and my model is
public class ResponseModel
{
[JsonProperty(PropertyName = "totalcount")]
public int totalcount { get; set; }
[JsonProperty(PropertyName = "tpa")]
public int tpa { get; set; }
[JsonProperty(PropertyName = "tip")]
public int tip { get; set; }
}
Please help thank you.
you have an extra ":" at the end of property name of your json, so try this json property names. This code was tested in Visual Studio and working properly
ResponseModel result = null;
if ( responseMessage.IsSuccessStatusCode)
{
var json = await responseMessage.Content.ReadAsStringAsync();
var jsonObject = JObject.Parse(json);
var data=jsonObject["data"];
if (data!=null) result = data.ToObject<ResponseModel>();
}
public class ResponseModel
{
[JsonProperty("totalcount:")]
public int totalcount { get; set; }
[JsonProperty("tpa:")]
public int tpa { get; set; }
[JsonProperty("tip:")]
public int tip { get; set; }
}
or you can fix an API
In my model I added the colon ":" since the return value of the properties in the API has a ":" colon per property.
public class ResponseModel
{
[JsonProperty(PropertyName = "totalcount:")]
public int totalcount { get; set; }
[JsonProperty(PropertyName = "tpa:")]
public int tpa { get; set; }
[JsonProperty(PropertyName = "tip:")]
public int tip { get; set; }
}

How to JSON Serialize a List with Extension in C#

public class ParentData :ObservableRangeCollection<ChildData>
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
public class ChildData
{
public string ChildName { get; set; }
public int ChildAge { get; set; }
}
List<ParentData> lstParent = new List<ParentData>();
ParentData pData = new ParentData();
pData.Name = "John";
pData.Id = 111;
pData.Age = 45;
ChildData childData = new ChildData();
childData.ChildAge = 12;
childData.ChildName = "tommy";
pData.Add(childData);
lstParent.Add(pData);
string Json = JsonConvert.SerializeObject(lstParent, Formatting.None);
Upon serializing, the ParentData does not get serialized
[[{"ChildName":"tommy","ChildAge":12,"ErrorMessage":null}]]
How do I serialize the Parent Node?

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 JSON data in to a class in c#

hello people I have this Json data:
https://openexchangerates.org/api/latest.json?app_id=6cf59607a32d408eb3e04de1427a3169
and I want to deserialize in the following class
using Newtonsoft.Json;
using System.Collections.Generic;
namespace Divisas2MVVM2.Classes
{
public class ExchangeRates
{
[JsonProperty(PropertyName = "disclaimer")]
public string Disclaimer { get; set; }
[JsonProperty(PropertyName = "license")]
public string License { get; set; }
[JsonProperty(PropertyName = "timestamp")]
public int TimeStamp { get; set; }
[JsonProperty(PropertyName = "base")]
public string Base { get; set; }
[JsonProperty(PropertyName = "rates")]
public Rates Rates { get; set; }
}
public class Rates
{
public double AED { get; set; }
public double AFN { get; set; }
public double ALL { get; set; }
public double AMD { get; set; }
// I cut the text so that it would not be to long
public double ZMW { get; set; }
public double ZWL { get; set; }
}
public class Rate
{
public double TaxRate { get; set; }
public string Code { get; set; }
}
this is my attribute
private ExchangeRates exchangeRates;
the constructor of my MainViewModel
new ObservableCollection data
Rates = new ObservableCollection<Rate>();
and in this method a get the json data
try
{
var client = new HttpClient();
client.BaseAddress = new Uri("https://openexchangerates.org");
var url = "/api/latest.json?app_id=6cf59607a32d408eb3e04de1427a3169";
var response = await client.GetAsync(url);
if (!response.IsSuccessStatusCode)
{
Message = response.StatusCode.ToString();
IsRunning = false;
return;
}
var result = await response.Content.ReadAsStringAsync();
exchangeRates = JsonConvert.DeserializeObject<ExchangeRates>(result);
}
everything works fine, the variable result has correctly the json data in a string format, but when i call JsonConvert . DeserializeObject, the data "rates" it is not assigned correctly, all the other data: disclaimer", "license", "timestamp" etc. is correctly assigned. only rates fail.
the string is correct
other data is correct in the class
rates is incorrect
sorry for my English I hope you have understood me :)
use this as your model class
namespace Rate
{
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public partial class Rates
{
[JsonProperty("disclaimer")]
public string Disclaimer { get; set; }
[JsonProperty("license")]
public string License { get; set; }
[JsonProperty("timestamp")]
public long Timestamp { get; set; }
[JsonProperty("base")]
public string Base { get; set; }
[JsonProperty("rates")]
public Dictionary<string, double> RatesRates { get; set; }
}
public partial class Rates
{
public static Rates FromJson(string json) => JsonConvert.DeserializeObject<Rates>(json, Rate.Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this Rates self) => JsonConvert.SerializeObject(self, Rate.Converter.Settings);
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters = {
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}
}
Then do this in your class
var data = Rate.Rates.FromJson("jsonresult");
var rate = data.RatesRates;
foreach (var pair in rate)
{
string symbol = pair.Key; //"AED"
double value = pair.Value; //3.673175,
}
var time = data.Timestamp;
var disclaimer = data.Disclaimer;
var license = data.License;
Tested and working

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