I currently tried to get serialized response from a RestSharp PostAsync call like in
var responseData = Client.PostAsync<Data>(request).Result;
Now, this is what I receive:
{
"status":1,
"success":"message transmitted",
"available":19215,
"message_ids":"26684730:56798"
}
and this is the "Data" class:
public class Data
{
[JsonProperty("status")]
public int Status { get; set; }
[JsonProperty("success")]
public string Success { get; set; }
[JsonProperty("available")]
public int Available { get; set; }
[JsonProperty("message_ids")]
public string MessageIds { get; set; }
[JsonProperty("error")]
public string Error { get; set; }
}
I don't know why, but the property message_ids is always null!?
May this be caused by the : in the string, and my this be a bug in RestSharp?
Here is what "Data" looks like:
for restsharp you need JsonPropertyName attribute
[JsonPropertyName("message_ids")]
public string MessageIds { get; set; }
or if you want to use JsonProperty you will have to use Newtonsoft.Json
var response = client.ExecuteAsync(request).Result;
//if you have async method better to use
var response = await client.ExecuteAsync(request);
Data data = JsonConvert.DeserializeObject<Data>(response.Content);
I'm calling an API that returns a standard response structure for all actions. I deserialise the response using Newtonsoft.Json and JsonConvert.DeserializeObject<Response>
public class Response
{
public int Code { get; set; }
public string Message { get; set; }
public object Result { get; set; }
public DateTime ResponseDateTime { get; set; }
}
The Result object changes depending on the request action that has been requested and if there was an error or not (Code + Message describe errors). I know the structure of the Result objects and have created classes for each Result I require.
How do i go about casting the Result object to a typed variable such as a Detail[]? I know i can serialize the Result object and deserialise it again but surely there is a more elegant solution.
public class Detail
{
public int Id { get; set; }
public string DetailOne { get; set; }
public string DetailTwo { get; set; }
}
If you always know what kind of Result you'll get, I'd suggest making Response generic:
public class Response<TResult>
{
public int Code { get; set; }
public string Message { get; set; }
public T Result { get; set; }
public DateTime ResponseDateTime { get; set; }
}
Then you can just deserialize to a Response<Detail[]> and Json.NET should handle everything fine.
This is assuming that if there's an error, you won't end up with a Result that could be problematic. Alternatively, you could still deserialize to a Response class, but with a JToken property type:
public class Response
{
public int Code { get; set; }
public string Message { get; set; }
public JToken Result { get; set; }
public DateTime ResponseDateTime { get; set; }
}
Then use:
Response response = JsonConvert.DeserializeObject<Response>(json);
if (response.Code == 200) // Or whatever
{
Detail[] details = response.Result.ToObject<Detail[]>();
}
You could potentially wrap that into a generic method. I'd at least try the generic response class first though.
If you change your object model to this:
public class Response<T>
{
public int Code { get; set; }
public string Message { get; set; }
public T Result { get; set; }
public DateTime ResponseDateTime { get; set; }
}
public class Detail
{
public int Id { get; set; }
public string DetailOne { get; set; }
public string DetailTwo { get; set; }
}
Then you can do this:
var original = new Response<Detail>()
{
Code = 42,
Message = "OK",
Result = new Detail()
{
Id = 1701,
DetailOne = "Don't",
DetailTwo = "Panic",
},
ResponseDateTime = DateTime.Now,
};
var json = JsonConvert.SerializeObject(original, Newtonsoft.Json.Formatting.Indented);
var response = JsonConvert.DeserializeObject<Response<Newtonsoft.Json.Linq.JToken>>(json);
if (response.Code == 42)
{
Detail detail = response.Result.ToObject<Detail>();
/* Do something with `Detail`. */
}
That seems a fairly nice way to get to the underlying Detail object.
The key technique is to serialize a Response<Detail> and deserialize as a Response<JToken>. Simple.
Of course, if you really wanted to create a Response<Detail> instance you could make a fairly straightforward way of converting from Response<JToken> to Response<Detail> by doing a straight field-to-field mapping.
Here is the Response<Detail[]> version of the code:
var original = new Response<Detail[]>()
{
Code = 42,
Message = "OK",
Result = new Detail[]
{
new Detail()
{
Id = 1701,
DetailOne = "Don't",
DetailTwo = "Panic",
},
new Detail()
{
Id = 360,
DetailOne = "Microsoft",
DetailTwo = "Xbox",
}
},
ResponseDateTime = DateTime.Now,
};
var json = JsonConvert.SerializeObject(original, Newtonsoft.Json.Formatting.Indented);
var response = JsonConvert.DeserializeObject<Response<Newtonsoft.Json.Linq.JToken>>(json);
if (response.Code == 42)
{
Detail[] detail = response.Result.ToObject<Detail[]>();
/* Do something with `Detail[]`. */
}
I am trying to set a class for a token using DeserializeObject from the json object i get back from my api. However when i run the below code it sets all the values to null or 0, not the result i am getting from the api.
cs code
var resultString = await result.Content.ReadAsStringAsync();
var post = JsonConvert.DeserializeObject<Token>(resultString);
class
public class Token : ContentPage
{
public int StaffID { get; set; }
public string TokenApi { get; set; }
public string StaffForename { get; set; }
public string StaffSurname { get; set; }
public string StaffEmail { get; set; }
public int PrimaryStaffRoleID { get; set; }
}
JSON response
"{\"code\":201,\"status\":\"Success\",\"message\":\"Object found\",\"data\":{\"StaffID\":14,\"StaffSurname\":\"Test\",\"StaffForename\":\"Test\",\"StaffEmail\":\"test#test.com\",\"PrimaryStaffRoleID\":5,\"TokenApi\":\"testToken\"}}"
Firstly the data which you are trying to map is inside another property in your json called Data and secondly your json does not have a property with name Token
The problem actually is you are not using the correct type that reflects your json, means you don't have correct c# type which would get mapped to json, you can generate correct types using json2charp.com , the correct classes for it are :
public class Data
{
public int StaffID { get; set; }
public string StaffSurname { get; set; }
public string StaffForename { get; set; }
public string StaffEmail { get; set; }
public int PrimaryStaffRoleID { get; set; }
public string TokenApi { get; set; }
}
public class RootObject
{
public int code { get; set; }
public string status { get; set; }
public string message { get; set; }
public Data data { get; set; }
}
Now deserializing using RootObject as type parameter would work perfectly fine like:
var resultString = await result.Content.ReadAsStringAsync();
var post = JsonConvert.DeserializeObject<RootObject>(resultString);
A more good option is to use QuickType.IO which would even generate code for you in c# or any other language that they are supporting.
If you analyze the JSON that you posted, the object that you're trying to Deserialize is inside the "data" property of your json.
I suggest you creating a class to represent the JsonResponse with a Data property. This will be your Token
You are retrieved a string that match this object
public string code {get;set;}
public string Success {get;set;} ...
And Token is matching data in json, so
var post = JsonConvert.DeserializeObject<Token>(resultString.data);
would be better.
I have the following code
public async Task<ActionResult> GetExtendedProperties()
{
Uri serviceRoot = new Uri(SettingsHelper.AzureAdGraphApiEndPoint);
var token = AppToken.GetAppToken();
var adClient = AuthenticationHelper.GetActiveDirectoryClient();
Microsoft.Azure.ActiveDirectory.GraphClient.Application app = (Microsoft.Azure.ActiveDirectory.GraphClient.Application)adClient.Applications.Where(
a => a.AppId == SettingsHelper.ClientId).ExecuteSingleAsync().Result;
if (app == null)
{
throw new ApplicationException("Unable to get a reference to application in Azure AD.");
}
string requestUrl = string.Format("https://graph.windows.net/{0}/applications/{1}/extensionProperties?api-version=1.5", SettingsHelper.Tenant, app.ObjectId);
HttpClient hc = new HttpClient();
hc.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
"Bearer", token);
HttpResponseMessage hrm = await hc.GetAsync(new Uri(requestUrl));
if (hrm.IsSuccessStatusCode)
{
string jsonresult = await hrm.Content.ReadAsStringAsync();
return View();
}
else
{
return View();
}
}
and it returns this string
{"odata.metadata":"https://graph.windows.net/mysaasapp.onmicrosoft.com/$metadata#directoryObjects/Microsoft.DirectoryServices.ExtensionProperty","value":[{"odata.type":"Microsoft.DirectoryServices.ExtensionProperty","objectType":"ExtensionProperty","objectId":"f751a646-2cc1-4e30-bfc6-a8217c0ce0a3","deletionTimestamp":null,"appDisplayName":"","name":"extension_33e037a7b1aa42ab96936c22d01ca338_Modulos","dataType":"String","isSyncedFromOnPremises":false,"targetObjects":["User"]},{"odata.type":"Microsoft.DirectoryServices.ExtensionProperty","objectType":"ExtensionProperty","objectId":"80aabe1b-b020-41d1-bd2d-cc04af264fe5","deletionTimestamp":null,"appDisplayName":"","name":"extension_33e037a7b1aa42ab96936c22d01ca338_ModulesPerUser","dataType":"String","isSyncedFromOnPremises":false,"targetObjects":["User"]},{"odata.type":"Microsoft.DirectoryServices.ExtensionProperty","objectType":"ExtensionProperty","objectId":"6e3d7592-7a66-4792-b408-891251197868","deletionTimestamp":null,"appDisplayName":"","name":"extension_33e037a7b1aa42ab96936c22d01ca338_Comasdasa3dsdaspInfo","dataType":"String","isSyncedFromOnPremises":false,"targetObjects":["User"]},{"odata.type":"Microsoft.DirectoryServices.ExtensionProperty","objectType":"ExtensionProperty","objectId":"93a26374-4135-4f29-9f24-4154522449ec","deletionTimestamp":null,"appDisplayName":"","name":"extension_33e037a7b1aa42ab96936c22d01ca338_CompInfo","dataType":"String","isSyncedFromOnPremises":false,"targetObjects":["User"]},{"odata.type":"Microsoft.DirectoryServices.ExtensionProperty","objectType":"ExtensionProperty","objectId":"21a8a3d4-f4b4-45b4-8d07-55d450db35f2","deletionTimestamp":null,"appDisplayName":"","name":"extension_33e037a7b1aa42ab96936c22d01ca338_CompanyNameForSaasApp","dataType":"String","isSyncedFromOnPremises":false,"targetObjects":["User"]},{"odata.type":"Microsoft.DirectoryServices.ExtensionProperty","objectType":"ExtensionProperty","objectId":"7b3109e0-8710-4d1a-81c3-2b6a83fb62ee","deletionTimestamp":null,"appDisplayName":"","name":"extension_33e037a7b1aa42ab96936c22d01ca338_Compania","dataType":"String","isSyncedFromOnPremises":false,"targetObjects":["User"]}]}
Using json2charp, I created this class:
public class ActiveDirectorySchemaExtension
{
public string objectType { get; set; }
public string objectId { get; set; }
public object deletionTimestamp { get; set; }
public string appDisplayName { get; set; }
public string name { get; set; }
public string dataType { get; set; }
public bool isSyncedFromOnPremises { get; set; }
public List<string> targetObjects { get; set; }
}
How can I convert that string into a List so that I can easy manipulate it on the view?
Update:
I tried this:
List<ActiveDirectorySchemaExtension> tmp = JsonConvert.DeserializeObject<List<ActiveDirectorySchemaExtension>>(jsonresult);
but I got this
Cannot deserialize the current JSON object (e.g. {"name":"value"})
into type
'System.Collections.Generic.List`1[CapatechSaasApp.Areas.GlobalAdmin.Models.ActiveDirectorySchemaExtension]'
because the type requires a JSON array (e.g. [1,2,3]) to deserialize
correctly. To fix this error either change the JSON to a JSON array
(e.g. [1,2,3]) or change the deserialized type so that it is a normal
.NET type (e.g. not a primitive type like integer, not a collection
type like an array or List) that can be deserialized from a JSON
object. JsonObjectAttribute can also be added to the type to force it
to deserialize from a JSON object. Path '['odata.metadata']', line 1,
position 18.
You forgot your parent object in json2csharp.
Demo on .NETFiddle
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
public class ActiveDirectorySchemaExtension // You can switch from the original class name to yours
{
public string Type { get; set; } // You should switch to PascalCase to respect C# notation
public string ObjectType { get; set; }
public string ObjectId { get; set; }
public object DeletionTimestamp { get; set; }
public string AppDisplayName { get; set; }
public string Name { get; set; }
public string DataType { get; set; }
public bool IsSyncedFromOnPremises { get; set; }
public List<string> TargetObjects { get; set; }
}
public class RootObject
{
public string Metadata { get; set; }
public List<ActiveDirectorySchemaExtension> Value { get; set; }
}
public void Main()
{
var json = "{'odata.metadata':'https://graph.windows.net/mysaasapp.onmicrosoft.com/$metadata#directoryObjects/Microsoft.DirectoryServices.ExtensionProperty','value':[{'odata.type':'Microsoft.DirectoryServices.ExtensionProperty','objectType':'ExtensionProperty','objectId':'f751a646-2cc1-4e30-bfc6-a8217c0ce0a3','deletionTimestamp':null,'appDisplayName':'','name':'extension_33e037a7b1aa42ab96936c22d01ca338_Modulos','dataType':'String','isSyncedFromOnPremises':false,'targetObjects':['User']},{'odata.type':'Microsoft.DirectoryServices.ExtensionProperty','objectType':'ExtensionProperty','objectId':'80aabe1b-b020-41d1-bd2d-cc04af264fe5','deletionTimestamp':null,'appDisplayName':'','name':'extension_33e037a7b1aa42ab96936c22d01ca338_ModulesPerUser','dataType':'String','isSyncedFromOnPremises':false,'targetObjects':['User']},{'odata.type':'Microsoft.DirectoryServices.ExtensionProperty','objectType':'ExtensionProperty','objectId':'6e3d7592-7a66-4792-b408-891251197868','deletionTimestamp':null,'appDisplayName':'','name':'extension_33e037a7b1aa42ab96936c22d01ca338_Comasdasa3dsdaspInfo','dataType':'String','isSyncedFromOnPremises':false,'targetObjects':['User']},{'odata.type':'Microsoft.DirectoryServices.ExtensionProperty','objectType':'ExtensionProperty','objectId':'93a26374-4135-4f29-9f24-4154522449ec','deletionTimestamp':null,'appDisplayName':'','name':'extension_33e037a7b1aa42ab96936c22d01ca338_CompInfo','dataType':'String','isSyncedFromOnPremises':false,'targetObjects':['User']},{'odata.type':'Microsoft.DirectoryServices.ExtensionProperty','objectType':'ExtensionProperty','objectId':'21a8a3d4-f4b4-45b4-8d07-55d450db35f2','deletionTimestamp':null,'appDisplayName':'','name':'extension_33e037a7b1aa42ab96936c22d01ca338_CompanyNameForSaasApp','dataType':'String','isSyncedFromOnPremises':false,'targetObjects':['User']},{'odata.type':'Microsoft.DirectoryServices.ExtensionProperty','objectType':'ExtensionProperty','objectId':'7b3109e0-8710-4d1a-81c3-2b6a83fb62ee','deletionTimestamp':null,'appDisplayName':'','name':'extension_33e037a7b1aa42ab96936c22d01ca338_Compania','dataType':'String','isSyncedFromOnPremises':false,'targetObjects':['User']}]}";
var o = JsonConvert.DeserializeObject<RootObject>(json);
Console.WriteLine(o.Value[0].Name);
}
output :
extension_33e037a7b1aa42ab96936c22d01ca338_Modulos
You can use DataContractSerializer
var serializer = new DataContractJsonSerializer(typeof(RootObject));
var ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
var root = serializer.ReadObject(ms) as RootObject;
But also you should mark your classes with [DataContract] attribute and members with [DataMember] attribute.
Like this:
[DataContract]
public class RootObject
{
[DataMember(Name = "odata.metadata")]
public string metadata { get; set; }
[DataMember]
public List<ActiveDirectorySchemaExtension> value { get; set; }
}
Also, you've lost one field in your class ActiveDirectorySchemaExtension - odataType. You can deserialize it like this:
[DataMember(Name = "odata.type")]
public string odataType { get; set; }
I try to convert from json to c# class object, cause my final step will be put this all data into local db. I have no problem with taking this data like one big string, so the Url is good typed, but it's not my goal.
this is the json data, here we have two objects
[{"id":"1","category":"1","name":"good 1","prize":"12.3","prize2":"13.4","elements":"row,column,paper","secid":"2131","description":"nice","quality":"best","dateofcoming":"2013-12-20 18:08:50","date":"2013-12-20 00:00:00"},
{"id":"2","category":"2","name":"good","prize":"14.3","prize2":"15.4","elements":"up,down,left","secid":"2132","description":"nc","quality":"best","dateofcoming":"2013-12-20 18:10:55","date":"2013-12-20 00:00:00"}]
I try by this way which I found in the book:
private void getBookData()
{
// we creating the request details to the site which give us
// back the json data
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("here is my adress");
request.Accept = "and header";
// starting the request
request.BeginGetResponse(callback_with_food_info, request);
}
private void callback_with_food_info(IAsyncResult ar)
{
HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
// we get the response
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(ar);
DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(ListOfObjects));
Stream responseStream = response.GetResponseStream();
ListOfObjects listOfobj = (ListOfObjects)deserializer.ReadObject(responseStream);
String fsdfsdf = "how how how";
}
and in the row when we ReadObject
ListOfObjects listOfobj = (ListOfObjects)deserializer.ReadObject(responseStream);
I get exception "System.InvalidCastException", my List class look like this and it was created by http://json2csharp.com/
public class DetailsOfObject
{
public string id { get; set; }
public string category { get; set; }
public string name { get; set; }
public string prize { get; set; }
public string prize2 { get; set; }
public string elements { get; set; }
public string secid { get; set; }
public string description { get; set; }
public string quality { get; set; }
public string dateofcoming { get; set; }
public string date { get; set; }
}
public class ListOfObjects
{
public DetailsOfObject list {get; set;}
}
Any advice how to finally convert this json data
You need to add Data attributes to model
[DataContract]
public class DetailsOfObject
{
[DataMember]
public string id { get; set; }
and change deserialize line
List<DetailsOfObject> listOfobj = (List<DetailsOfObject>)deserializer.ReadObject(responseStream);