Passing a location to web api using json - json

[DataContract]
public partial class Event
{
[DataMember]
public int EventId { get; set; }
[DataMember]
[Required]
[StringLength(50)]
public string UserId { get; set; }
[DataMember]
[Required]
public string EventDescription { get; set; }
[DataMember]
[Column(TypeName = "smalldatetime")]
public DateTime Timestamp { get; set; }
[DataMember]
public int ActivityId { get; set; }
[DataMember]
public DbGeography Location { get; set; }
[IgnoreDataMember]
public virtual Activity Activity { get; set; }
}
I have the above class in web api and my get all and get by id functions work. Here is an example of the json they return:
{
"EventId": 6,
"UserId": "1",
"EventDescription": "Auckland city test",
"Timestamp": "2015-02-11T00:00:00",
"ActivityId": 1,
"Location": {
"Geography": {
"CoordinateSystemId": 4326,
"WellKnownText": "POINT (180.756117 -30.85824)"
}
}
}
However if i pass the same json back to the post method it is not able to convert the location to a dbgeography type. I have tried formatting the location in different ways but always get a nullReferenceException in location.
Is there a particular way I should format the location in json? I would have thought that if it serializes to that format it would be able to de-serialize that format.
Thanks

Related

ASPNET Core - Json response stops at the related model

I have two classes that have relations between them. They are Market and Promotion classes. I'm facing a problem when I make a request. The json result stops when it comes to the relation.
The Market class:
public class Market : BaseModel
{
[Required]
public string Name { get; set; }
[Required]
public string Address { get; set; }
// GPS informations.
public double Latitude { get; set; }
public double Longitude { get; set; }
[Required]
public Guid PictureId { get; set; }
public Picture Picture { get; set; }
public List<Promotion> Promotions { get; set; }
}
The Promotion class:
public class Promotion : BaseModel
{
[Required]
public string Description { get; set; }
[Required]
public double Price { get; set; }
[Required]
public Guid PictureId { get; set; }
public Picture Picture { get; set; }
[Required]
public Guid MarketId { get; set; }
public Market Market { get; set; }
}
When I make the next request, I got an incomplete answer.
[HttpGet]
[AllowAnonymous]
public async Task<ActionResult<IEnumerable<Market>>> Get()
{
var markets = await _context.Markets
.Include(m => m.Owner)
.Include(m => m.Picture)
.Include(m => m.Promotions)
.ToListAsync();
return markets;
}
The response json stops when get at MarketId of the first promotion.
...
"pictureType": 0,
"pictureUrl": "https://superbarato.azurewebsites.net/api/Pictures/url/d6bc07a8-db55-4ee5-7342-08d73f6147e9",
"id": "d6bc07a8-db55-4ee5-7342-08d73f6147e9",
"createdAt": "2019-09-22T13:34:26.9367403",
"updatedAt": "0001-01-01T00:00:00",
"deletedAt": "0001-01-01T00:00:00",
"ownerId": "75c1f286-c07f-4e50-dda0-08d73f61058f",
"owner": null
},
"promotions": [
{
"description": "Açúcar Camil 1Kg",
"price": 5.0,
"pictureId": "e7af68b9-c053-4f4b-7344-08d73f6147e9",
"picture": null,
"marketId": "e2962be8-1a19-418a-6ce7-08d73f62308d"
How to get all the promotions?
In EF Core , you could configure Json.NET to ignore cycles that it finds in the object graph. This is done in the ConfigureServices(...) method in Startup.cs.
services.AddMvc()
.AddJsonOptions(
options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
Another alternative is to decorate one of the navigation properties with the [JsonIgnore] attribute, which instructs Json.NET to not traverse that navigation property while serializing.
Reference :https://learn.microsoft.com/en-us/ef/core/querying/related-data#related-data-and-serialization
The problem of yours is that Market and Promotion types/instances are referencing each other and you got into a cycle serializing those two indefinitely. You may solve it by projecting database model without relationship to response model/structure to avoid that.
[HttpGet]
[AllowAnonymous]
public async Task<ActionResult<IEnumerable<MarketModel>>> Get()
{
var markets = await _context.Markets
.Select(m => new MarketModel {
Name = m.Name,
// Other properties needed to be serialized to response body
Promotions = m.Promotions.Select(p => new PromotionModel {
Description = p.Description,
// Other properties needed to be serialized to response body
MarketId = p.Market.Id
}
}
.ToListAsync();
return markets;
}
public class MarketModel
{
public string Name { get; set; }
// Other properties needed to be serialized to response body
public List<PromotionModel> Promotions { get; set; }
}
public class PromotionModel
{
public string Description { get; set; }
// Other properties needed to be serialized to response body
public Guid MarketId { get; set; }
}
Hope it helps.

How to Deserialize Complex JSON file in SSIS into few columns

I have a JSON file that is pretty complex. Here is a snippet of my file:
JSON
"SKU": "12345",
"Status": {
"Health": "OK"
},
"Type": "ComputerSystem",
"Name": "Cartridge 1",
"Power": "Off",
"AssetTag": "12345",
"HostCorrelation": {
"IPAddress": [],
"HostMACAddress": [
"00:00:00:00:00:00",
"11:11:11:11:11:11"
]
},
"SerialNumber": "12345",
"Boot": {
"BootSourceOverrideSupported": [
"None",
"PXE",
"HDD",
"iSCSI",
"M.2",
"None"
],
"BootSourceOverrideTarget": "PXE",
"BootSourceOverrideEnabled": "Continuous"
}
Without showing all the classes here is the RootObject VS generates as code:
Paste JSON as Class
public class Rootobject
{
public string SKU { get; set; }
public Status Status { get; set; }
public string Type { get; set; }
public string Name { get; set; }
public string Power { get; set; }
public string AssetTag { get; set; }
public Hostcorrelation HostCorrelation { get; set; }
public string SerialNumber { get; set; }
public Boot Boot { get; set; }
public Links links { get; set; }
public string UUID { get; set; }
public Bios Bios { get; set; }
public Oem Oem { get; set; }
public Memory Memory { get; set; }
public Availableaction[] AvailableActions { get; set; }
public string SystemType { get; set; }
public Processors Processors { get; set; }
public string Model { get; set; }
public string Manufacturer { get; set; }
}
I want to loop through multiple JSON files with this structure, and put them into a few columns such as (Section, Component, Property, and Value).
However, I have been having a hard time figuring this out. It would be simple to put each part into its own unique column.
The end result of my JSON example above may look like:
Goal SQL Output
The format doesn't have to be exact, but something along those lines. If there is a better way of doing this I am all ears.
I can tell you didn't post all of your classes because the RootObject has object references, but this is how you could start your code. This won;t get your data into the format you asked for, but it is how the serializer works.
string json = [Somehow get your json in to a string]
JavaScriptSerializer js = new JavaScriptSerializer();
var jRow = js.Deserialize<Rootobject>(json);
// now you have your entire JSON in one object.
//for the data you presented you will need a few outputs:
// let's start with the outermost:
Output0Buffer.AddRow();
Output0Buffer.SKU = jRow.SKU;
Output0Buffer.Health = jRow.Status.Health; //There is only one option here
Output0Buffer.Type = jRow.Type ;
Output0Buffer.Name = jRow.Name;
Output0Buffer.Power = jRow.Power ;
Output0Buffer.AssetTag = jRow.AssetTag ;
Output0Buffer.SerialNumber = jRow.SerialNumber ;
Output0Buffer.BootSourceOverrideTarget= jRow.Boot.BootSourceOverrideTarget ;
Output0Buffer.BootSourceOverrideEnabled= jRow.Boot.BootSourceOverrideEnabled;
//this is a new output of boot details linked by SKU
foreach(var dtl in jRow.Boot.BootSourceOverrideSupported)
{
OutputBootStuffBuffer.AddRow();
OutputBootStuffBuffer.SKU = JRow.SKU; //Making assumption that SKU is the key back
OutputBootStuffBuffer.BootSourceOverrideSupported = dtl.BootSourceOverrideSupported;
}

Extracting JSON data using Newtonsoft in xamarin.android

Hello I am getting JSON data from server and i want to extract that JSON in Xamarin. How can i parse that JSON using NewTonSoft
below is the JSON responce i receive
[
{
"Id": 5,
"AlbumKey": "2REC2ZDSFK",
"ZipFillPath": "aaaa#gmail.com\\2REC2ZDSFK",
"NoOfPages": 3,
"EmailID": "aaaa#gmail.com"
}
]
This should be your Model
public class RootObject
{
public int Id { get; set; }
public string AlbumKey { get; set; }
public string ZipFillPath { get; set; }
public int NoOfPages { get; set; }
public string EmailID { get; set; }
}
Then
RootObject myObj = JsonConvert.DeserializeObject<RootObject>(json);
If your json is a List of objects, something like
List<RootObject> myListObj = JsonConvert.DeserializeObject<List<RootObject>>(json);
public class yourClass
{
public int Id { get; set; }
public string AlbumKey { get; set; }
public string ZipFillPath { get; set; }
public int NoOfPages { get; set; }
public string EmailID { get; set; }
}
Considering this as your model class you can
var responseText= JsonConvert.DeserializeObject<yourClass>(jsonResponse);
Then depending on if its a list or a not you can get the data from it
In case you are unable to find the class what you can do is check if the namespace of your current class and that class is the same.

Parse Json to List

I want to parse a json to List how can we do that. I have tried the following code but it didnt worked
Dictionary<string, object> pGateways=(Dictionary<string,object>)Json.JsonParser.FromJson(jsonString);
List<object> creditOptions = new List<object>();
creditOptions = (List<object>)pGateways;
And after getting it int list i want to loop through it
Here is my sample json
{
"MessageCode": "CS2009",
"Status": "Y",
"ErrorCode": "0",
"ErrorDescription": "Success",
"account":
{
"card":
[
{
"cardend": "asd",
"token": "aads",
"cardstart": "asdad",
"accounttype": "asda",
"cardnetwork": "as",
"issuer": "asd",
"customername": "a",
"expdate": "04/2018"
},
{
"cardend": "asda",
"token":"adssadsa",
"cardstart": "asd",
"accounttype": "asd",
"cardnetwork": "asd",
"issuer": "asda",
"customername": "asd",
"expdate": "03/2016"
}
],
"bank": []
}
}
The best option could be to use the JsonConvert in order to parse Json into a List.
Reference: JSON Parsing in Windows Phone
You can use Json.Net.
To install Json.NET use NugetGallery : Json.net Nugets Gallery
And you can use json2Csharp.com for generate c# classes from json
The JSON string you posted is not suitable for straight-forward deserialization to List. The easiest thing to do is use the online JSON 2 CSharp tool to generate classes and deserialize the json string to it. Here is an example of the generated classes:
public class Card
{
public string cardend { get; set; }
public string token { get; set; }
public string cardstart { get; set; }
public string accounttype { get; set; }
public string cardnetwork { get; set; }
public string issuer { get; set; }
public string customername { get; set; }
public string expdate { get; set; }
}
public class Account
{
public List<Card> card { get; set; }
public List<object> bank { get; set; }
}
public class RootObject
{
public string MessageCode { get; set; }
public string Status { get; set; }
public string ErrorCode { get; set; }
public string ErrorDescription { get; set; }
public Account account { get; set; }
}
And here is the logic for deserialization:
var root = JsonConvert.DeserializeObject<RootObject>(jsonStr);
where the jsonStr variable holds the json string you posted.
You need to use json2csharp tool to generate classes and deserialize the JSON string to list.
Here is the Classes generated from your JSON string.
public class Card
{
public string cardend { get; set; }
public string token { get; set; }
public string cardstart { get; set; }
public string accounttype { get; set; }
public string cardnetwork { get; set; }
public string issuer { get; set; }
public string customername { get; set; }
public string expdate { get; set; }
}
public class Account
{
public List<Card> card { get; set; }
public List<object> bank { get; set; }
}
public class RootObject
{
public string MessageCode { get; set; }
public string Status { get; set; }
public string ErrorCode { get; set; }
public string ErrorDescription { get; set; }
public Account account { get; set; }
}
and Deserialize your JSON object using JsonConvert,
Suppose e.result is your JSON string then
var rootObject = JsonConvert.DeserializeObject<RootObject>(e.Result);
foreach (var blog in rootObject.Card)
{
//access your data like this- `blog.cardend;` or `blog.token;`
}

Deserialize OneNote JSon result

I'm trying to deserialize some OneNote API results. Below is my:
Example result from notebook query
Sample Class
Code to deserialize (two attempts obj1 and obj2
Content-Type: application/json
X-CorrelationId: <GUID>
Status: 200 OK
{
"#odata.context":"https://www.onenote.com/api/v1.0/$metadata#notebooks","value":[
{
"isDefault":false,
"userRole":"Contributor",
"isShared":true,
"sectionsUrl":"https://www.onenote.com/api/v1.0/notebooks/notebook ID/sections",
"sectionGroupsUrl":"https://www.onenote.com/api/v1.0/notebooks/notebook ID/sectionGroups",
"links":{
"oneNoteClientUrl":{
"href":"https:{client URL}"
},"oneNoteWebUrl":{
"href":"https://{web URL}"
}
},
"id":"notebook ID",
"name":"notebook name",
"self":"https://www.onenote.com/api/v1.0/notebooks/notebook ID",
"createdBy":"user name",
"lastModifiedBy":"user name",
"createdTime":"2013-10-05T10:57:00.683Z",
"lastModifiedTime":"2014-01-28T18:49:00.47Z"
},{
"isDefault":true,
"userRole":"Owner",
"isShared":false,
"sectionsUrl":"https://www.onenote.com/api/v1.0/notebooks/notebook ID/sections",
"sectionGroupsUrl":"https://www.onenote.com/api/v1.0/notebooks/notebook ID/sectionGroups",
"links":{
"oneNoteClientUrl":{
"href":"https://{client URL}"
},"oneNoteWebUrl":{
"href":"https://{web URL}"
}
},
"id":"notebook ID",
"name":"notebook name",
"self":"https://www.onenote.com/api/v1.0/notebooks/notebook ID",
"createdBy":"user name",
"lastModifiedBy":"user name",
"createdTime":"2011-07-20T03:54:46.283Z",
"lastModifiedTime":"2014-06-24T20:49:42.227Z"
}
]
}
[DataContract]
public class Notebooks
{
[DataMember]
public bool isDefault { get; set; }
[DataMember]
public string userRole { get; set; }
[DataMember]
public string isShared { get; set; }
[DataMember]
public string sectionsUrl { get; set; }
[DataMember]
public string sectionGroupsUrl { get; set; }
[DataMember]
public string oneNoteWebUrl { get; set; }
[DataMember]
public string name { get; set; }
[DataMember]
public string self { get; set; }
[DataMember]
public string createdBy { get; set; }
[DataMember]
public string lastModifiedBy { get; set; }
[DataMember]
public string lastModifiedTime { get; set; }
[DataMember]
public string id { get; set; }
[DataMember]
public string createdTime { get; set; }
}
// This sample web string returned from the Web Request is stored in this textbox
string resultStr = resultTextBox.Text.ToString();
var obj1 = DeserializeJSon<List<Notebooks>>(resultStr);
foreach (Notebooks nb in obj1)
{
string id = nb.ToString();
}
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<Notebooks>));
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(resultStr));
var obj2 = (List<Notebooks>)ser.ReadObject(stream);
foreach (Notebooks nb in obj2)
{
string id = nb.id.ToString();
}
public static T DeserializeJSon<T>(string jsonString)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
T obj = (T)ser.ReadObject(stream);
return obj;
}
You are not being able to deserialize the result because the JSON returned by the OneNote API is not a List of Notebook objects. It is one main object with two properties: "#odata.context" and "value". "value" itself is a list of Notebook objects.
I suggest you make a class like the following
public class OneNoteJsonResponse
{
[DataMember(Name = "#odata.context")]
public string ODataContext {get; set;}
[DataMember]
public List<Notebook> value {get; set;}
}
Then try deserializing the response using DataContractSerializer following this example:
Deserialize JSON with C#
I personally would recommend using JSON.NET instead of the DataContractSerializer, as it provides more flexibility and better performance. You can install it easily using nuget.
http://james.newtonking.com/json
Let us know if you have any issues, happy coding!
EDIT: Also, the Notebook object you have is missing the "links" object, which would be another class of its own. (containing the oneNoteClientUrl and OneNoteWebUrl)
response string cannot be used verbatim with your data model. start from "value".
alternatively, have a look at http://json2csharp.com/
hth