I have a JSON API result that I processed thru an online JSON-to-C# structure program to create the class structure. I've used this many times for other projects. The JSON returned everything along with a public class RootObject that references both the status and payload segments of the returned values.
I am using ASP.NET C# library to deserialize the result JSON using JavaScriptSerializer:
var vlist = new JavaScriptSerializer().Deserialize<TestStruct>(result);
My data structure looks like this (it's pretty standard):
public class TestStruct
{
public class Status
{
public int statusCode { get; set; }
public int errorType { get; set; }
public int errorCode { get; set; }
public string errorMessage { get; set; }
}
public class Payload
{
public VehicleStatusRpt vehicleStatusRpt { get; set; }
}
public class VehicleStatusRpt
{
public string statusType { get; set; }
//public ReportDate reportDate { get; set; }
//public VehicleStatus vehicleStatus { get; set; }
}
public class RootObject
{
public Status status { get; set; }
public Payload payload { get; set; }
}
}
The full JSON Result I'm trying to parse using the class structure is:
{
"status": {
"statusCode": 0,
"errorType": 0,
"errorCode": 0,
"errorMessage": "Success with response body"
},
"payload": {
"vehicleSummary": [
{
"vin": "KNDJX3AE8E7000080",
"vehicleIdentifier": "000080",
"modelName": "SOUL EV",
"modelYear": "2015",
"nickName": "My SOUL",
"generation": 1,
"extColorCode": "1D",
"trim": "EV",
"imagePath": {
"imageName": "2015-soul_ev-ev-1d.png",
"imagePath": "/content/dam/kia/us/owners/image/vehicle/2015/soul_ev/ev/",
"imageType": "1",
"imageSize": {
"length": "100",
"width": "100",
"uom": 0
}
},
"enrollmentStatus": 1,
"fatcAvailable": 1,
"telematicsUnit": 1,
"fuelType": 4,
"colorName": "CLEAR WHITE",
"activationType": 1,
"mileage": "24410",
"dealerCode": "MOBISDLR1",
"mobileStore": [
{
"osType": 0,
"downloadURL": "https://itunes.apple.com/us/app/kia-access-with-uvo-link/id1280548773?mt=8",
"image": {
"imageName": "iosImage.png",
"imagePath": "/content/dam/kia/us/owners/image/common/app/",
"imageType": "2",
"imageSize": {
"length": "100",
"width": "100",
"uom": 0
}
}
},
{
"osType": 1,
"downloadURL": "https://play.google.com/store/apps/details?id=com.myuvo.link",
"image": {
"imageName": "androidImage.png",
"imagePath": "/content/dam/kia/us/owners/image/common/app/",
"imageType": "2",
"imageSize": {
"length": "100",
"width": "100",
"uom": 0
}
}
}
],
"supportedApp": {
"appType": "5",
"appImage": {
"imageName": "app-access.png",
"imagePath": "/content/dam/kia/us/owners/image/common/app/access/",
"imageType": "2",
"imageSize": {
"length": "100",
"width": "100",
"uom": 0
}
}
},
"supportAdditionalDriver": 0,
"customerType": 0,
"vehicleKey": "937db044-8328-4188-a3d2-68ac3b183752"
}
]
}
}
I run this thru json2csharp.com to get the structure (the sample above is an abbreviated 'test' only
The deserializer returns an error: Invalid JSON Primitive (starting with Payload)
I see examples of using RootObject but with the Newtonsoft JSON libary. I would like to use the Microsoft library. Do I really need to switch to Newtonsoft JSON? If I can use JavaScriptSerializer library, how?
The classes that correspond to the JSON you posted are:
public class RootObject
{
public Status status { get; set; }
public Payload payload { get; set; }
}
public class Status
{
public int statusCode { get; set; }
public int errorType { get; set; }
public int errorCode { get; set; }
public string errorMessage { get; set; }
}
public class Payload
{
public List<VehicleSummary> vehicleSummary { get; set; }
}
public class VehicleSummary
{
public string vin { get; set; }
public string vehicleIdentifier { get; set; }
public string modelName { get; set; }
public string modelYear { get; set; }
public string nickName { get; set; }
public int generation { get; set; }
public string extColorCode { get; set; }
public string trim { get; set; }
public Image imagePath { get; set; }
public int enrollmentStatus { get; set; }
public int fatcAvailable { get; set; }
public int telematicsUnit { get; set; }
public int fuelType { get; set; }
public string colorName { get; set; }
public int activationType { get; set; }
public string mileage { get; set; }
public string dealerCode { get; set; }
public List<MobileStore> mobileStore { get; set; }
public SupportedApp supportedApp { get; set; }
public int supportAdditionalDriver { get; set; }
public int customerType { get; set; }
public string vehicleKey { get; set; }
}
public class Image
{
public string imageName { get; set; }
public string imagePath { get; set; }
public string imageType { get; set; }
public ImageSize imageSize { get; set; }
}
public class ImageSize
{
public string length { get; set; }
public string width { get; set; }
public int uom { get; set; }
}
public class MobileStore
{
public int osType { get; set; }
public string downloadURL { get; set; }
public Image image { get; set; }
}
public class SupportedApp
{
public string appType { get; set; }
public Image appImage { get; set; }
}
I was able to deserialize the JSON just fine using JavaScriptSerializer like this:
var root = new JavaScriptSerializer().Deserialize<RootObject>(result);
where result is the JSON string you posted in your question.
Note, however, that if you have placed your classes inside another class called TestStruct then you would need to take that into account and deserialize to TestStruct.RootObject instead, e.g.:
var root = new JavaScriptSerializer().Deserialize<TestStruct.RootObject>(result);
I was also able to deserialize the JSON using Json.Net in the same way with the JsonConvert class:
var root = JsonConvert.DeserializeObject<RootObject>(result);
Once you have the deserialized object, you can extract some interesting information from it like this:
foreach (var vs in root.payload.vehicleSummary)
{
Console.WriteLine(string.Format("{0} - {1} {2} {3}, {4} mi",
vs.vin, vs.colorName, vs.modelYear, vs.modelName, vs.mileage));
}
Here is a working demo using Json.Net: https://dotnetfiddle.net/Zh35be
I have a JSON object with a dynamic key for the properties I wish to map to a class. I'm uncertain how to build my class to deserialize with JSON.NET. I need the values from the 'results' and 'more' keys at the upper level and also the the values from the 'timesheets' key.
Here is my JSON data.
{
"results": {
"timesheets": {
"7994790": {
"id": 7994790,
"user_id": 165502,
"jobcode_id": 11267673,
"start": "2019-12-20T05:48:00-05:00",
"end": "2019-12-20T13:44:00-05:00",
"duration": 28560,
"date": "2019-12-20",
"tz": -5,
"tz_str": "tsET",
"type": "regular",
"location": "Android App",
"on_the_clock": false,
"locked": 0,
"notes": "",
"customfields": {
"20251": "",
"19647": "Laborer",
"20327": "",
"19648": ""
},
"last_modified": "2019-12-20T20:28:48+00:00",
"attached_files": [],
"created_by_user_id": 165502
},
"8087496": {
"id": 8087496,
"user_id": 165502,
"jobcode_id": 2415904,
"start": "2019-12-20T13:44:00-05:00",
"end": "2019-12-20T15:11:00-05:00",
"duration": 5220,
"date": "2019-12-20",
"tz": -5,
"tz_str": "tsET",
"type": "regular",
"location": "Android App",
"on_the_clock": false,
"locked": 0,
"notes": "",
"customfields": {
"20251": "",
"19647": "Laborer",
"20327": "",
"19648": ""
},
"last_modified": "2019-12-20T20:28:49+00:00",
"attached_files": [],
"created_by_user_id": 165502
}
}
},
"more": false
}
And my classes as I currently have them which returns this error.
System.ArgumentNullException: 'Value cannot be null.
Parameter name: values'
public class RootObject
{
public Results results { get; set; }
public bool more { get; set; }
}
public class Results
{
public Timesheets timesheets { get; set; }
}
public class Timesheets
{
public Dictionary<int, TimesheetDetails> timesheetsdetails { get; set; }
}
public class TimesheetDetails
{
public int id { get; set; }
public int user_id { get; set; }
public int jobcode_id { get; set; }
public DateTime start { get; set; }
public DateTime end { get; set; }
public int duration { get; set; }
public string date { get; set; }
public int tz { get; set; }
public string tz_str { get; set; }
public string type { get; set; }
public string location { get; set; }
public bool on_the_clock { get; set; }
public int locked { get; set; }
public string notes { get; set; }
public DateTime last_modified { get; set; }
public List<object> attached_files { get; set; }
public int created_by_user_id { get; set; }
}
var stuff = JsonConvert.DeserializeObject<RootObject>(result.Content);
Console.WriteLine(string.Join(",", stuff.results.timesheets.timesheetsdetails));
First of all your json is not valid. After the end of the first timesheet data, there should be a , You can use any json validator online simply to validate it.
And in your model the Results is not valid according to the json. The below one works for me.
public class Response
{
[JsonProperty("results")] public TimeSheetResponse TimeSheetResponse { get; set; }
[JsonProperty("more")] public bool More { get; set; }
}
public class TimeSheetResponse
{
[JsonProperty("timesheets")] public Dictionary<string, Timesheet> Timesheets { get; set; }
}
public class Timesheet
{
[JsonProperty("id")]
public long Id { get; set; }
[JsonProperty("user_id")]
public long UserId { get; set; }
[JsonProperty("jobcode_id")]
public long JobcodeId { get; set; }
[JsonProperty("start")] public DateTimeOffset Start { get; set; }
[JsonProperty("end")] public DateTimeOffset End { get; set; }
[JsonProperty("duration")] public long Duration { get; set; }
[JsonProperty("date")] public DateTimeOffset Date { get; set; }
[JsonProperty("tz")] public long Tz { get; set; }
[JsonProperty("tz_str")] public string TzStr { get; set; }
[JsonProperty("type")] public string Type { get; set; }
[JsonProperty("location")] public string Location { get; set; }
[JsonProperty("on_the_clock")] public bool OnTheClock { get; set; }
[JsonProperty("locked")] public long Locked { get; set; }
[JsonProperty("notes")] public string Notes { get; set; }
[JsonProperty("customfields")] public Dictionary<string, string> Customfields { get; set; }
[JsonProperty("last_modified")] public DateTimeOffset LastModified { get; set; }
[JsonProperty("attached_files")] public List<object> AttachedFiles { get; set; }
[JsonProperty("created_by_user_id")]
public long CreatedByUserId { get; set; }
}
To deserialize to Object..
var timeSheetResult = JsonConvert.DeserializeObject<Response>(data);
I've seen this other question with no answer.
I need to deserialize with newtonsoft because I have circular references loop handling when sending the data to the client:
services.AddMvc()
.AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
options.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.Objects;
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
});
This produces JSON objects with $id and $ref that are making the model binder fail on POST requests when the modified data comes back.
Note that I have a pretty complex object model as you can se below:
{
"$id": "1",
"vehiculeId": 359586015016459,
"companyId": 1,
"phoneNumber": "0614407055",
"hardwareId": 866857042994208,
"gpsBoxType": 29,
"name": "Redmi Note 6 Pro Olivier M",
"iconId": 33554432,
"gpsBoxTrackingDelay": 1,
"rowEnabled": true,
"dbInsertTime": 1549527854940,
"device": {
"$id": "2",
"imei": 866857042994208,
"brand": "Xiaomi",
"model": "Redmi Note 6 Pro",
"name": "Redmi Olivier",
"registration": {
"$id": "3",
"imei": 866857042994208,
"companyId": 1,
"latestRegistrationStatusChangeDate": 1556168520000,
"registrationStatus": 3,
"imeiNavigation": {
"$ref": "2"
}
}
},
"vehiculeGpsBoxInfo": {
"$id": "4",
"vehiculeId": 359586015016459,
"currentDelay": 0,
"normalTrackingMode": 2,
"timeModeDelay": 60000,
"smartModeDelay": 5000,
"smartModeDistance": 100,
"connected": false,
"heartBeatPeriod": 1200,
"insureCoherence": false,
"killHedgehog": true,
"lastCommunicationTime": 1563519489853,
"vehiculeConfiguration": 111,
"updateStatus": 0,
"vehicule": {
"$ref": "1"
},
"vehiculeConfigurationNavigation": {
"$id": "5",
"configurationId": 111,
"configName": "PTIMobileTest",
"masterName": "PTIMobileTest",
"master": "1.0.32",
"firmware": "",
"system": "",
"gpsBoxType": 29,
"vehiculeGpsBoxInfoBoardConfigurationNavigation": [],
"vehiculeGpsBoxInfoVehiculeConfigurationNavigation": [
{
"$ref": "4"
}
]
}
},
"fleetDetail": [
{
"$id": "6",
"fleetDetailId": 27494,
"fleetId": 3216,
"vehiculeId": 359586015016459,
"rowEnabled": true,
"fleet": {
"$id": "7",
"fleetId": 3216,
"companyId": 1,
"name": "RTEProtect",
"rowEnabled": true,
"fleetDetailFleet": [
{
"$id": "8",
"fleetDetailId": 25988,
"fleetId": 3216,
"vehiculeId": 359586014995819,
"rowEnabled": true,
"fleet": {
"$ref": "7"
},
"vehicule": {
"$id": "9",
"vehiculeId": 359586014995819,
"companyId": 1,
"phoneNumber": " ",
"hardwareId": 358240051111110,
"gpsBoxType": 29,
"category": "PTI",
"name": "PTI Emulateur Fred",
"iconId": 33571442,
"gpsBoxTrackingDelay": 1,
"addressProtocol": 0,
"rowEnabled": true,
"dbInsertTime": 1448607218600,
"device": {
"$id": "10",
"imei": 358240051111110,
"brand": "Unknown",
"model": "PTI Emulateur",
"name": "PTI Emulateur Fred",
"registration": {
"$id": "11",
"imei": 358240051111110,
"companyId": 1,
"latestRegistrationStatusChangeDate": 1556169720000,
"registrationStatus": 3,
"imeiNavigation": {
"$ref": "10"
}
}
},
"vehiculeGpsBoxInfo": {
"$id": "12",
"vehiculeId": 359586014995819,
"currentDelay": 0,
"normalTrackingMode": 2,
"timeModeDelay": 60000,
"smartModeDelay": 5000,
"smartModeDistance": 100,
"connected": false,
"heartBeatPeriod": 1200,
"insureCoherence": false,
"killHedgehog": true,
"boardConnected": false,
"lastCommunicationTime": 1558078487030,
"boardLastCommunicationTime": 1485957716897,
"vehiculeConfiguration": 169,
"updateStatus": 0,
"vehicule": {
"$ref": "9"
},
"vehiculeConfigurationNavigation": {
"$id": "13",
"configurationId": 169,
"configName": "SafeProtectSamsungS6",
"masterName": "SafeProtectSamsungS6",
"master": "1.0.2",
"firmware": "",
"system": "",
"gpsBoxType": 29,
"vehiculeGpsBoxInfoBoardConfigurationNavigation": [],
"vehiculeGpsBoxInfoVehiculeConfigurationNavigation": [
{
"$ref": "12"
},
{
"$id": "14",
"vehiculeId": 359586015014836,
"currentDelay": 0,
"normalTrackingMode": 2,
"timeModeDelay": 60000,
"smartModeDelay": 5000,
"smartModeDistance": 100,
"connected": true,
"heartBeatPeriod": 1200,
"insureCoherence": false,
"killHedgehog": true,
"lastCommunicationTime": 1568202416063,
"vehiculeConfiguration": 169,
"updateStatus": 0,
"lastConnexionTime": 1545057192063,
"vehicule": {
"$id": "15",
"vehiculeId": 359586015014836,
"companyId": 1,
"phoneNumber": "+351927853883",
"hardwareId": 356437083709729,
"gpsBoxType": 29,
"name": "Samsung J5 test",
"iconId": 33569462,
"gpsBoxTrackingDelay": 1,
"addressProtocol": 0,
"rowEnabled": true,
"dbInsertTime": 1513945397863,
"device": {
"$id": "16",
"imei": 356437083709729,
"brand": "Samsung",
"model": "J5",
"name": "Test"
},
"vehiculeGpsBoxInfo": {
"$ref": "14"
},
"fleetDetail": [
{
"$id": "17",
"fleetDetailId": 26445,
"fleetId": 3486,
"vehiculeId": 359586015014836,
"rowEnabled": true,
"fleet": {
"$id": "18",
"fleetId": 3486,
"companyId": 1,
"name": "RTE Protect",
"rowEnabled": true,
"fleetDetailFleet": [
{
"$id": "19",
"fleetDetailId": 13251,
"fleetId": 3486,
"vehiculeId": 359586014995819,
"rowEnabled": true,
"fleet": {
"$ref": "18"
},
"vehicule": {
"$ref": "9"
}
},
{
"$id": "20",
"fleetDetailId": 13477,
"fleetId": 3486,
"vehiculeId": 359586014995819,
"rowEnabled": false,
"fleet": {
"$ref": "18"
},
"vehicule": {
"$ref": "9"
}
},
{
"$ref": "17"
}
],
"fleetDetailFleetChildNavigation": []
},
"vehicule": {
"$ref": "15"
}
},
{
"$id": "21",
"fleetDetailId": 27485,
"fleetId": 5536,
"vehiculeId": 359586015014836,
"rowEnabled": true,
"fleet": {
"$id": "22",
"fleetId": 5536,
"companyId": 1,
"name": "SafeProtect",
"rowEnabled": true,
"fleetDetailFleet": [
{
"$ref": "21"
}
],
"fleetDetailFleetChildNavigation": []
},
"vehicule": {
"$ref": "15"
}
}
]
},
"vehiculeConfigurationNavigation": {
"$ref": "13"
}
},
{
"$id": "23",
"vehiculeId": 359586015014947,
"currentDelay": 0,
"normalTrackingMode": 2,
"timeModeDelay": 60000,
"smartModeDelay": 5000,
"smartModeDistance": 100,
"connected": false,
"heartBeatPeriod": 1200,
"insureCoherence": false,
"killHedgehog": true,
"lastCommunicationTime": 1558006034977,
"vehiculeConfiguration": 169,
"updateStatus": 0,
"vehicule": {
"$id": "24",
"vehiculeId": 359586015014947,
"companyId": 1,
"phoneNumber": " ",
"hardwareId": 357011072523992,
"gpsBoxType": 29,
"name": "SafeProtect Céline Pro",
"iconId": 33554432,
"gpsBoxTrackingDelay": 1,
"addressProtocol": 0,
"rowEnabled": true,
"dbInsertTime": 1538134175170,
"device": {
"$id": "25",
"imei": 357011072523992,
"brand": "Samsung",
"model": "S6",
"name": "S6 Céline",
"registration": {
"$id": "26",
"imei": 357011072523992,
"companyId": 1,
"latestRegistrationStatusChangeDate": 1556103600000,
"registrationStatus": 3,
"imeiNavigation": {
"$ref": "25"
}
}
},
"vehiculeGpsBoxInfo": {
"$ref": "23"
},
"fleetDetail": []
},
"vehiculeConfigurationNavigation": {
"$ref": "13"
}
}
]
}
},
"fleetDetail": [
{
"$id": "27",
"fleetDetailId": 26334,
"fleetId": 659,
"vehiculeId": 359586014995819,
"rowEnabled": false,
"fleet": {
"$id": "28",
"fleetId": 659,
"companyId": 1,
"name": "Parc",
"rowEnabled": true,
"fleetDetailFleet": [
{
"$ref": "27"
}
],
"fleetDetailFleetChildNavigation": []
},
"vehicule": {
"$ref": "9"
}
},
{
"$ref": "8"
},
{
"$id": "29",
"fleetDetailId": 25993,
"fleetId": 3216,
"vehiculeId": 359586014995819,
"rowEnabled": false,
"fleet": {
"$ref": "7"
},
"vehicule": {
"$ref": "9"
}
},
{
"$ref": "19"
},
{
"$ref": "20"
}
]
}
},
{
"$ref": "29"
},
{
"$ref": "6"
}
],
"fleetDetailFleetChildNavigation": []
},
"vehicule": {
"$ref": "1"
}
},
{
"$id": "30",
"fleetDetailId": 27476,
"fleetId": 5534,
"vehiculeId": 359586015016459,
"rowEnabled": true,
"fleet": {
"$id": "31",
"fleetId": 5534,
"companyId": 1,
"name": "Ol",
"rowEnabled": true,
"fleetDetailFleet": [
{
"$ref": "30"
}
],
"fleetDetailFleetChildNavigation": []
},
"vehicule": {
"$ref": "1"
}
}
]
}
Currently my controller method is the following:
[HttpPost]
public async Task<IActionResult> UpdateDati([FromBody] Vehicule model)
{...}
I'm surprised there is no simple way to sepcify this kind of behavior for model binding. What am I missing here?
The solution is to write a custom model binder to use JSON.Net to deserialize the request body:
/// <summary>
/// Custom model binder to be used when TrackableEntities coming in HttpPost Methods.
/// </summary>
public class TrackableEntityModelBinder : IModelBinder
{
/// <inheritdoc/>
public async Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException(nameof(bindingContext));
}
using (var reader = new StreamReader(bindingContext.HttpContext.Request.Body))
{
var body = await reader.ReadToEndAsync().ConfigureAwait(continueOnCapturedContext: false);
// Do something
var value = JsonConvert.DeserializeObject(body, bindingContext.ModelType, new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
ContractResolver = new CamelCasePropertyNamesContractResolver(),
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
});
bindingContext.Result = ModelBindingResult.Success(value);
}
}
}
Then, just anotate your HttpPost method parameter to use the model binder:
[HttpPost]
public async Task<IActionResult> UpdateDati([ModelBinder(typeof(TrackableEntityModelBinder))] [FromBody] Vehicule model)
{
}
Can you try removing them:
.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.None;
does your Vehicule object match (I just used paste special in VS)
public class Rootobject
{
public string id { get; set; }
public long vehiculeId { get; set; }
public int companyId { get; set; }
public string phoneNumber { get; set; }
public long hardwareId { get; set; }
public int gpsBoxType { get; set; }
public string name { get; set; }
public int iconId { get; set; }
public int gpsBoxTrackingDelay { get; set; }
public bool rowEnabled { get; set; }
public long dbInsertTime { get; set; }
public Device device { get; set; }
public Vehiculegpsboxinfo vehiculeGpsBoxInfo { get; set; }
public Fleetdetail[] fleetDetail { get; set; }
}
public class Device
{
public string id { get; set; }
public long imei { get; set; }
public string brand { get; set; }
public string model { get; set; }
public string name { get; set; }
public Registration registration { get; set; }
}
public class Registration
{
public string id { get; set; }
public long imei { get; set; }
public int companyId { get; set; }
public long latestRegistrationStatusChangeDate { get; set; }
public int registrationStatus { get; set; }
public Imeinavigation imeiNavigation { get; set; }
}
public class Imeinavigation
{
public string _ref { get; set; }
}
public class Vehiculegpsboxinfo
{
public string id { get; set; }
public long vehiculeId { get; set; }
public int currentDelay { get; set; }
public int normalTrackingMode { get; set; }
public int timeModeDelay { get; set; }
public int smartModeDelay { get; set; }
public int smartModeDistance { get; set; }
public bool connected { get; set; }
public int heartBeatPeriod { get; set; }
public bool insureCoherence { get; set; }
public bool killHedgehog { get; set; }
public long lastCommunicationTime { get; set; }
public int vehiculeConfiguration { get; set; }
public int updateStatus { get; set; }
public Vehicule vehicule { get; set; }
public Vehiculeconfigurationnavigation vehiculeConfigurationNavigation { get; set; }
}
public class Vehicule
{
public string _ref { get; set; }
}
public class Vehiculeconfigurationnavigation
{
public string id { get; set; }
public int configurationId { get; set; }
public string configName { get; set; }
public string masterName { get; set; }
public string master { get; set; }
public string firmware { get; set; }
public string system { get; set; }
public int gpsBoxType { get; set; }
public object[] vehiculeGpsBoxInfoBoardConfigurationNavigation { get; set; }
public Vehiculegpsboxinfovehiculeconfigurationnavigation[] vehiculeGpsBoxInfoVehiculeConfigurationNavigation { get; set; }
}
public class Vehiculegpsboxinfovehiculeconfigurationnavigation
{
public string _ref { get; set; }
}
public class Fleetdetail
{
public string id { get; set; }
public int fleetDetailId { get; set; }
public int fleetId { get; set; }
public long vehiculeId { get; set; }
public bool rowEnabled { get; set; }
public Fleet fleet { get; set; }
public Vehicule7 vehicule { get; set; }
}
public class Fleet
{
public string id { get; set; }
public int fleetId { get; set; }
public int companyId { get; set; }
public string name { get; set; }
public bool rowEnabled { get; set; }
public Fleetdetailfleet[] fleetDetailFleet { get; set; }
public object[] fleetDetailFleetChildNavigation { get; set; }
}
public class Fleetdetailfleet
{
public string id { get; set; }
public int fleetDetailId { get; set; }
public int fleetId { get; set; }
public long vehiculeId { get; set; }
public bool rowEnabled { get; set; }
public Fleet1 fleet { get; set; }
public Vehicule1 vehicule { get; set; }
public string _ref { get; set; }
}
public class Fleet1
{
public string _ref { get; set; }
}
public class Vehicule1
{
public string id { get; set; }
public long vehiculeId { get; set; }
public int companyId { get; set; }
public string phoneNumber { get; set; }
public long hardwareId { get; set; }
public int gpsBoxType { get; set; }
public string category { get; set; }
public string name { get; set; }
public int iconId { get; set; }
public int gpsBoxTrackingDelay { get; set; }
public int addressProtocol { get; set; }
public bool rowEnabled { get; set; }
public long dbInsertTime { get; set; }
public Device1 device { get; set; }
public Vehiculegpsboxinfo1 vehiculeGpsBoxInfo { get; set; }
public Fleetdetail2[] fleetDetail { get; set; }
}
public class Device1
{
public string id { get; set; }
public long imei { get; set; }
public string brand { get; set; }
public string model { get; set; }
public string name { get; set; }
public Registration1 registration { get; set; }
}
public class Registration1
{
public string id { get; set; }
public long imei { get; set; }
public int companyId { get; set; }
public long latestRegistrationStatusChangeDate { get; set; }
public int registrationStatus { get; set; }
public Imeinavigation1 imeiNavigation { get; set; }
}
public class Imeinavigation1
{
public string _ref { get; set; }
}
public class Vehiculegpsboxinfo1
{
public string id { get; set; }
public long vehiculeId { get; set; }
public int currentDelay { get; set; }
public int normalTrackingMode { get; set; }
public int timeModeDelay { get; set; }
public int smartModeDelay { get; set; }
public int smartModeDistance { get; set; }
public bool connected { get; set; }
public int heartBeatPeriod { get; set; }
public bool insureCoherence { get; set; }
public bool killHedgehog { get; set; }
public bool boardConnected { get; set; }
public long lastCommunicationTime { get; set; }
public long boardLastCommunicationTime { get; set; }
public int vehiculeConfiguration { get; set; }
public int updateStatus { get; set; }
public Vehicule2 vehicule { get; set; }
public Vehiculeconfigurationnavigation1 vehiculeConfigurationNavigation { get; set; }
}
public class Vehicule2
{
public string _ref { get; set; }
}
public class Vehiculeconfigurationnavigation1
{
public string id { get; set; }
public int configurationId { get; set; }
public string configName { get; set; }
public string masterName { get; set; }
public string master { get; set; }
public string firmware { get; set; }
public string system { get; set; }
public int gpsBoxType { get; set; }
public object[] vehiculeGpsBoxInfoBoardConfigurationNavigation { get; set; }
public Vehiculegpsboxinfovehiculeconfigurationnavigation1[] vehiculeGpsBoxInfoVehiculeConfigurationNavigation { get; set; }
}
public class Vehiculegpsboxinfovehiculeconfigurationnavigation1
{
public string _ref { get; set; }
public string id { get; set; }
public long vehiculeId { get; set; }
public int currentDelay { get; set; }
public int normalTrackingMode { get; set; }
public int timeModeDelay { get; set; }
public int smartModeDelay { get; set; }
public int smartModeDistance { get; set; }
public bool connected { get; set; }
public int heartBeatPeriod { get; set; }
public bool insureCoherence { get; set; }
public bool killHedgehog { get; set; }
public long lastCommunicationTime { get; set; }
public int vehiculeConfiguration { get; set; }
public int updateStatus { get; set; }
public long lastConnexionTime { get; set; }
public Vehicule3 vehicule { get; set; }
public Vehiculeconfigurationnavigation2 vehiculeConfigurationNavigation { get; set; }
}
public class Vehicule3
{
public string id { get; set; }
public long vehiculeId { get; set; }
public int companyId { get; set; }
public string phoneNumber { get; set; }
public long hardwareId { get; set; }
public int gpsBoxType { get; set; }
public string name { get; set; }
public int iconId { get; set; }
public int gpsBoxTrackingDelay { get; set; }
public int addressProtocol { get; set; }
public bool rowEnabled { get; set; }
public long dbInsertTime { get; set; }
public Device2 device { get; set; }
public Vehiculegpsboxinfo2 vehiculeGpsBoxInfo { get; set; }
public Fleetdetail1[] fleetDetail { get; set; }
}
public class Device2
{
public string id { get; set; }
public long imei { get; set; }
public string brand { get; set; }
public string model { get; set; }
public string name { get; set; }
public Registration2 registration { get; set; }
}
public class Registration2
{
public string id { get; set; }
public long imei { get; set; }
public int companyId { get; set; }
public long latestRegistrationStatusChangeDate { get; set; }
public int registrationStatus { get; set; }
public Imeinavigation2 imeiNavigation { get; set; }
}
public class Imeinavigation2
{
public string _ref { get; set; }
}
public class Vehiculegpsboxinfo2
{
public string _ref { get; set; }
}
public class Fleetdetail1
{
public string id { get; set; }
public int fleetDetailId { get; set; }
public int fleetId { get; set; }
public long vehiculeId { get; set; }
public bool rowEnabled { get; set; }
public Fleet2 fleet { get; set; }
public Vehicule5 vehicule { get; set; }
}
public class Fleet2
{
public string id { get; set; }
public int fleetId { get; set; }
public int companyId { get; set; }
public string name { get; set; }
public bool rowEnabled { get; set; }
public Fleetdetailfleet1[] fleetDetailFleet { get; set; }
public object[] fleetDetailFleetChildNavigation { get; set; }
}
public class Fleetdetailfleet1
{
public string id { get; set; }
public int fleetDetailId { get; set; }
public int fleetId { get; set; }
public long vehiculeId { get; set; }
public bool rowEnabled { get; set; }
public Fleet3 fleet { get; set; }
public Vehicule4 vehicule { get; set; }
public string _ref { get; set; }
}
public class Fleet3
{
public string _ref { get; set; }
}
public class Vehicule4
{
public string _ref { get; set; }
}
public class Vehicule5
{
public string _ref { get; set; }
}
public class Vehiculeconfigurationnavigation2
{
public string _ref { get; set; }
}
public class Fleetdetail2
{
public string id { get; set; }
public int fleetDetailId { get; set; }
public int fleetId { get; set; }
public long vehiculeId { get; set; }
public bool rowEnabled { get; set; }
public Fleet4 fleet { get; set; }
public Vehicule6 vehicule { get; set; }
public string _ref { get; set; }
}
public class Fleet4
{
public string id { get; set; }
public int fleetId { get; set; }
public int companyId { get; set; }
public string name { get; set; }
public bool rowEnabled { get; set; }
public Fleetdetailfleet2[] fleetDetailFleet { get; set; }
public object[] fleetDetailFleetChildNavigation { get; set; }
public string _ref { get; set; }
}
public class Fleetdetailfleet2
{
public string _ref { get; set; }
}
public class Vehicule6
{
public string _ref { get; set; }
}
public class Vehicule7
{
public string _ref { get; set; }
}
How can i extract this data in c#, in order to get all elements that i need:
{ "id": "PAY-6A414645MC669653MKPB2WCI", "create_time": "2014-07-14T10:03:53Z", "update_time": "2014-07-14T10:05:09Z", "intent": "sale", "payer": { "payment_method": "paypal", "payer_info": { "email": "severiano.testes#gmail.com", "first_name": "tester", "last_name": "tester", "payer_id": "MSQ6UB55W52N6", "shipping_address": { "line1": "1 Main Terrace", "line2": "", "city": "Wolverhampton", "country_code": "GB", "postal_code": "W12 4LQ", "state": "West Midlands" } } }, "transactions": [ { "amount": { "currency": "EUR", "total": "54.00", "details": { "subtotal": "54.00" } }, "description": "Transaction Description", "item_list": { "items": [ { "quantity": "1", "name": "Project Submission (featured)", "price": "54.00", "currency": "EUR", "sku": "64866" } ], "shipping_address": { "recipient_name": "tester tester", "line1": "1 Main Terrace", "line2": "", "city": "Wolverhampton", "country_code": "GB", "postal_code": "W12 4LQ", "state": "West Midlands" } }, "related_resources": [ { "sale": { "id": "4VV61663EL511901P", "create_time": "2014-07-14T10:03:53Z", "update_time": "2014-07-14T10:05:09Z", "amount": { "currency": "EUR", "total": "54.00" }, "state": "pending", "parent_payment": "PAY-6A414645MC669653MKPB2WCI", "links": [ { "href": "https://api.sandbox.paypal.com/v1/payments/sale/4VV61663EL511901P", "rel": "self", "method": "GET" }, { "href": "https://api.sandbox.paypal.com/v1/payments/sale/4VV61663EL511901P/refund", "rel": "refund", "method": "POST" }, { "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-6A414645MC669653MKPB2WCI", "rel": "parent_payment", "method": "GET" } ] } } ] } ], "state": "pending", "links": [ { "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-6A414645MC669653MKPB2WCI", "rel": "self", "method": "GET" } ] }
I've tried to send to jquery function but not successfull.
I've tried this aswell:
JObject.Parse(HttpContext.Current.Items["ResponseJson"]).GetValue("id");
this works but only to one value, not all that i want
you have to declare class like
public class Rootobject
{
public string id { get; set; }
public DateTime create_time { get; set; }
public DateTime update_time { get; set; }
public string intent { get; set; }
public Payer payer { get; set; }
public Transaction[] transactions { get; set; }
public string state { get; set; }
public Link1[] links { get; set; }
}
public class Payer
{
public string payment_method { get; set; }
public Payer_Info payer_info { get; set; }
}
public class Payer_Info
{
public string email { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string payer_id { get; set; }
public Shipping_Address shipping_address { get; set; }
}
public class Shipping_Address
{
public string line1 { get; set; }
public string line2 { get; set; }
public string city { get; set; }
public string country_code { get; set; }
public string postal_code { get; set; }
public string state { get; set; }
}
public class Transaction
{
public Amount amount { get; set; }
public string description { get; set; }
public Item_List item_list { get; set; }
public Related_Resources[] related_resources { get; set; }
}
public class Amount
{
public string currency { get; set; }
public string total { get; set; }
public Details details { get; set; }
}
public class Details
{
public string subtotal { get; set; }
}
public class Item_List
{
public Item[] items { get; set; }
public Shipping_Address1 shipping_address { get; set; }
}
public class Shipping_Address1
{
public string recipient_name { get; set; }
public string line1 { get; set; }
public string line2 { get; set; }
public string city { get; set; }
public string country_code { get; set; }
public string postal_code { get; set; }
public string state { get; set; }
}
public class Item
{
public string quantity { get; set; }
public string name { get; set; }
public string price { get; set; }
public string currency { get; set; }
public string sku { get; set; }
}
public class Related_Resources
{
public Sale sale { get; set; }
}
public class Sale
{
public string id { get; set; }
public DateTime create_time { get; set; }
public DateTime update_time { get; set; }
public Amount1 amount { get; set; }
public string state { get; set; }
public string parent_payment { get; set; }
public Link[] links { get; set; }
}
public class Amount1
{
public string currency { get; set; }
public string total { get; set; }
}
public class Link
{
public string href { get; set; }
public string rel { get; set; }
public string method { get; set; }
}
public class Link1
{
public string href { get; set; }
public string rel { get; set; }
public string method { get; set; }
}
Then you have to use using Newtonsoft.Json; for DeserializeObject like
var Data = JsonConvert.DeserializeObject<Rootobject>("your Json string");
you can access the property using Data
O/P looks like
You need to just make a mapping object and then deserialize the json
public class PaymentResponse
{
public string id { get; set; }
public DateTime create_time { get; set; }
public DateTime update_time { get; set; }
public string intent {get; set; }
public Payer payer{get; set; }
}
public class Payer
{
public string payment_method {get;set;}
public PayerInfo payer_info {get;set;}
}
public class PayerInfo
{
public string email { get; set; }
public string first_name { get; set; }
}
Don't have time to add all of the values but if you use a json parser so you can see the response easier. I prefer this one.
http://json.parser.online.fr/
Then simply deserialize the json response with the below
var paymentResponse = new JavaScriptSerializer().Deserialize<PaymentResponse>(jsonString);