Raw JSON for Key:Value - json

I have been looking for ages and found some similar questions but no answers which fixed my issue.
I have recently developed a Web API project with seperate server side and client side code. I am wanting to do some testing through apiary, mainly for posting data. This requires me to have a "sample" of the posted JSON. However i cannot seem to get a working sample (I am using Postman to test the JSON).
I am sending a ViewModel:
public class ExampleViewModel
{
public ExampleModel exampleModel { get; set; }
public String[] strings { get; set; }
}
With the model:
public class ExampleModel
{
public long ID { get; set; }
public string User { get; set; }
public string Title { get; set; }
public string Type { get; set; }
public string Description { get; set; }
public bool Recieved { get; set; }
public bool CompleteRemotly { get; set; }
public bool CompleteInField { get; set; }
public string Email { get; set; }
public Nullable<DateTime> RecievedDate { get; set; }
public Nullable<DateTime> ScheduledFor { get; set; }
public Nullable<DateTime> CompleteOn { get; set; }
public string Priority { get; set; }
public string DiagnosticReports { get; set; }
public string CReference { get; set; }
public string JReference { get; set; }
}
The format the POST is expecting is:
Key: "evm",
Value: {APIProject.ViewModels.ExampleViewModel}
And:
APIProject.ViewModels.ExampleViewModel:
{
strings = [""],
exampleModel = {ExampleModel}
}
My latest attempt is:
{
"evm":{
"ExampleViewModel":{
"exampleModel":{
"ID":0,
"User":"Harrison",
"Title":"Other",
"Type":"Other",
"Description":"ygfvgufuf",
"Recieved":true,
"CompleteRemotly":false,
"CompleteInField":false,
"Email":null,
"RecievedDate":"\/Date(1458133155546)\/",
"ScheduledFor":null,
"CompleteOn":null,
"Priority":"Low",
"DiagnosticReports":null,
"CPA_Reference":null,
"Jira_Reference":null,
"strings":[]
},
"strings":[""]
}
}
}
Which returns:
Key: "evm",
Value: {APIProject.ViewModels.ExampleViewModel}
And:
APIProject.ViewModels.ExampleViewModel:
{
strings = null,
exampleModel = null
}

So i got this to work by sending the following JSON, I guess i was over complicating the situation.
{
"requestManagements":{
"RequestID":0,
"User":"Harrison",
"RequestTitle":"Other",
"RequestType":"Other",
"RequestDescription":"ygfvgufuf",
"RequestRecieved":true,
"RequestCompleteRemotly":false,
"RequestCompleteInField":false,
"Email":null,
"RequestRecievedDate":"\/Date(1458133155546)\/",
"ScheduledFor":null,
"CompleteOn":null,
"Priority":"Low",
"DiagnosticReports":null,
"CPA_Reference":null,
"Jira_Reference":null,
"RequestManagerImages":[]
},
"images":[""]
}

Related

Why is my Post FromBody null in my API call?

I am getting null for no reason in my API. Why is this?
[AllowAnonymous]
[HttpPost("category")]
public async Task<IActionResult> PostProductCategory([FromBody] ProductCategoryViewModel message)
model:
public class ProductCategoryViewModel
{
public int? Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Advantages { get; set; }
public decimal? Liambda { get; set; }
public string Icon { get; set; }
public string UseCase { get; set; }
public string UseCaseTwo { get; set; }
public int? PackageType { get; set; }
}
json that i send :
{
advantages: "",
description: "",
icon: "",
id: 0,
liambda: "1111",
name: "asdfas",
packageType: 1,
useCase: "",
useCaseTwo: ""
}
Ok fixed the problem. It was quoted, I just copied from other place. the problem was when I enter data in JS I used input field "text" for decimal? value. and that is why it was not working.

Post Request gives a 405 error in Postman

on my .Net Web Api, I have an entity model, request model and response model for each database table. When i make a post request to MedTestOrder, i receive a 405 error, but other entities are working fine. I will post code snippets of MedTestOrder entity model, request model, response model, controller and service. The models contain foreign keys and I suspect my error could be from there. I might be wrong or not. I just need help.
1 - Entity Model
namespace Africanbiomedtests.Entities
{
public class MedTestOrder
{
[Key]
public int Id { get; set; }
public int? MedTestId { get; set; }
[ForeignKey("MedTestId")]
public MedTest MedTest { get; set; }
public int? healthcareProviderId { get; set; }
[ForeignKey("healthcareProviderId")]
public HealthcareProvider healthcareProvider { get; set; }
public int? accountId { get; set; }
[ForeignKey("accountId")]
public Account Account { get; set; }
public int? newbornId { get; set; }
[ForeignKey("newbornId")]
public Newborn Newborn { get; set; }
public string PaymentStatus { get; set; }
public Boolean CompletionStatus { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateCompleted { get; set; }
public DateTime? Updated { get; set; }
}
}
2 - Api Request Model
namespace Africanbiomedtests.Models.MedTestsOrder
{
public class MedTestsOrderCreateRequest
{
[Required]
public MedTest MedTest { get; set; }
public HealthcareProvider healthcareProvider { get; set; }
public Account Account { get; set; }
public Newborn Newborn { get; set; }
[Required]
public string PaymentStatus { get; set; }
public Boolean CompletionStatus { get; set; }
[Required]
public DateTime DateCreated { get; set; }
}
}
3 - Response Model
namespace Africanbiomedtests.Models.MedTestsOrder
{
public class MedTestsOrderResponse
{
public int Id { get; set; }
public MedTest MedTest { get; set; }
public HealthcareProvider healthcareProvider { get; set; }
public Account Account { get; set; }
public Newborn Newborn { get; set; }
public string PaymentStatus { get; set; }
public Boolean CompletionStatus { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateCompleted { get; set; }
}
}
4 - Controller
[Authorize]
[HttpPost("create")]
public ActionResult<MedTestsOrderResponse> Create(MedTestsOrderCreateRequest model)
{
var medTestOrder = _medTestOrderService.Create(model);
return Ok(medTestOrder);
}
5 - Service
public MedTestsOrderResponse Create(MedTestsOrderCreateRequest model)
{
// map model to new account object
var medTestOrder = _mapper.Map<MedTestOrder>(model);
medTestOrder.DateCreated = DateTime.UtcNow;
// save account
_context.MedTestOrder.Add(medTestOrder);
_context.SaveChanges();
return _mapper.Map<MedTestsOrderResponse>(medTestOrder);
}
6 - Postman JSON request
{
"medTestId": 3,
"healthcareProviderId": 3,
"accountId": 1004,
"newbornId": 1,
"paymentStatus": "Complete Payment",
"completionStatus": "1",
"dateCompleted": "01/14/2021"
}
I would really appreciate any help right now
Thank you #Marius and #Karney, I realized i wasn't using the correct endpoint. I needed to add "/create" to my endpoint.
In some cases, your Web API method accepts only PUT requests, Try to put not post.

How to deserialize multiple objects in Xamarin Forms? [duplicate]

This question already has answers here:
How to Deserialize JSON data? C#
(5 answers)
Closed 3 years ago.
I have two different class using that I am sending json response through API to another application, in short I am getting following json response :
{
"lmob_Forms":[
{
"Fields":"Certificate Name",
"Validation":"R",
"TabIndex":"1",
"FieldType":"DropdownList",
"DateFormate":"",
"Details":null
},
],
"drop_Salutation":[
{
"id":1,
"idvalue":"Kumari"
},
{
"id":2,
"idvalue":"Mrs"
},
{
"id":3,
"idvalue":"Ms"
}
]
}
I have searched regarding the same but not able to solve this problem, Need help, Thanks In Advance :)
First of all create a model of your json object.
public class LmobForm
{
public string Fields { get; set; }
public string Validation { get; set; }
public string TabIndex { get; set; }
public string FieldType { get; set; }
public string DateFormate { get; set; }
public object Details { get; set; }
}
public class DropSalutation
{
public int id { get; set; }
public string idvalue { get; set; }
}
public class RootObject
{
public List<LmobForm> lmob_Forms { get; set; }
public List<DropSalutation> drop_Salutation { get; set; }
}
Now, simply use Newtonsoft.Json to deserialize your JSON object as:
var myObj = JsonConvert.DeserializeObject<RootObject>(jsonString);
using json2csharp
public class LmobForm
{
public string Fields { get; set; }
public string Validation { get; set; }
public string TabIndex { get; set; }
public string FieldType { get; set; }
public string DateFormate { get; set; }
public object Details { get; set; }
}
public class DropSalutation
{
public int id { get; set; }
public string idvalue { get; set; }
}
public class RootObject
{
public List<LmobForm> lmob_Forms { get; set; }
public List<DropSalutation> drop_Salutation { get; set; }
}

Trying to check a json file for the correct parameters using JsonConvert.DeserisalizeObject<t>

I'm using the ACE code editor to gather Json and send it to my application. Once the Json hits the application, I need to make sure there are certain key's inside the json so I am using JsonConvert.DeserisalizeObject<t> to do this. Here's how:
public void SubmitReport(string JsonStringSend)
{
try
{
ReportItem RptItem = JsonConvert.DeserializeObject<ReportItem>(JsonStringSend);
}
catch(err)
{
return View(err);
}
}
and:
public class ReportItem
{
public Guid ReportID;
public bool RequiresFilter;
public string MimeType { get; set; }
public string ExternalID { get; set; }
public DateTime CreatedBy { get; set; }
public string ExecutionScript { get; set; }
public string ExecutionParameter { get; set; }
public string ExecutionOrderBy { get; set; }
public List<DynamicFilter> DynamicFilters { get; set; }
public bool RequiresOrgID { get; set; }
public QueryFilter ReportFilter { get; set; }
public QueryRule ReportRules { get; set; }
public List<QueryColumn> Columns { get; set; }
}
But for some reason, it bounces right over the catch even when I make sure some of the key's are incorrect. Am I not understanding the correct usage JsonConvert.DeserisalizeObject<t>? Or, is there a better way to be doing this check?
By default, the deserializer "tries it's best" to deserialize the object. But JSon.NET supports validation, the "straightforward" way is probably JSon Schema: http://www.newtonsoft.com/jsonschema.
Simple case can be handled with JSon.NET directly:
public class ReportItem
{
[JsonProperty(Required = Required.Always)]
public bool RequiresFilter;
[JsonProperty(Required = Required.Always)]
public string MimeType { get; set; }
public DateTime CreatedBy { get; set; }
public string ExecutionScript { get; set; }
public string ExecutionParameter { get; set; }
public string ExecutionOrderBy { get; set; }
public bool RequiresOrgID { get; set; }
}

How can I deserialization JSON in Windows Phone?

The first JSON is look like this
The second JSON is look like this
How can I deserialize them? I have been follow this example but it's not working.
Here's my code.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var w = new WebClient();
Observable
.FromEvent<DownloadStringCompletedEventArgs>(w, "DownloadStringCompleted")
.Subscribe(r =>
{
var deserialized =
JsonConvert.DeserializeObject<List<Place>>(r.EventArgs.Result);
PlaceList.ItemsSource = deserialized;
});
w.DownloadStringAsync(
new Uri("http://mobiilivantaa.lightscreenmedia.com/api/place"));
//For 2nd JSON
//w.DownloadStringAsync(
//new Uri("http://mobiilivantaa.lightscreenmedia.com/api/place/243"));
}
These are classes for 1st JSON.
public class Place
{
public string id { get; set; }
public string title { get; set; }
public string latitude { get; set; }
public string longitude { get; set; }
public string www { get; set; }
}
public class RootObjectJSON1
{
public List<Place> Places { get; set; }
}
These are classes for JSON2
public class Address
{
public string street { get; set; }
public string postal_code { get; set; }
public string post_office { get; set; }
}
public class Image
{
public string id { get; set; }
public string filename { get; set; }
public string path { get; set; }
}
public class RootObjectJSON2
{
public string id { get; set; }
public string title { get; set; }
public string description { get; set; }
public string latitude { get; set; }
public string longitude { get; set; }
public string www { get; set; }
public string phone { get; set; }
public string email { get; set; }
public string contact_person { get; set; }
public Address address { get; set; }
public List<Image> images { get; set; }
}
it looks that you should be deserializing object RootObjectJSON1 or RootObjectJSON2, e.g.:
var deserialized = JsonConvert.DeserializeObject<RootObjectJSON1>(r.EventArgs.Result);
Also it seems that collection Places should be with lowercase p at beginning or you need to tell Json.NET that this property should be deserialized with different name, e.g.:
[JsonProperty(PropertyName="places")]
public List<Place> Places { get; set; }
Generally I tend to use arrays for deserialization (from my experience works better) so I'll suggest to rewrite it to this:
public class RootObjectJSON1
{
public Place[] places { get; set; }
}
There is very good tool named json2csharp available at http://json2csharp.com/ - just put sample of JSON there and it will spit out classes in C# (doesn't detect DateTime so you might need to change that).