Deserializing JSON to flattened class - json

I found the same question here...
Deserializing nested JSON structure to a flattened class with Json.NET using annotations
...but without a proper answer. One of the best suggestion is to wrap the nested object in a new class but this approach introduces another issue: lego name. In my example the most logic name for this class is the same name that parent class and of course is not possible. My example is simple, I just want to eliminate the "language" property in the parent class. Can somebody help me to do it?
using Newtonsoft.Json;
public partial class NamedType
{
public string Name { get; set; }
}
public class Proficiency
{
public string Level { get; set; }
public string Name { get; set; }
}
public class Language
{
public int Id { get; set; }
public string Name { get; set; }
//public Language Language { get; set; } //Compiler error
//public Language Value { get; set; } //Not correct
//public NamedType Language { get; set; } //Compiler error
//public NamedType Value { get; set; } //Ugly, isn't?
public Proficiency Proficiency { get; set; }
}
List<Language> languageList = JsonConvert.DeserializeObject<List<Language>>(json);
Example of json:
{
"languages": [
{
"id": 1,
"language": { "name": "Spanish" },
"proficiency": {
"level": "native_or_bilingual",
"name": "Native or bilingual proficiency"
}
},
{
"id": 2,
"language": { "name": "English" },
"proficiency": {
"level": "full_professional",
"name": "Full professional proficiency"
}
},
{
"id": 3,
"language": { "name": "Japanese" },
"proficiency": {
"level": "elementary",
"name": "Elementary proficiency"
}
}
]
}

In cases where JSON property names conflict with c# naming conventions, you can use DataMember or JsonProperty annotations to substitute a different name during serialization.
For instance, the following works with both DataContractJsonSerializer and Json.NET:
[DataContract]
public class Language
{
[DataContract]
class NamedType
{
[DataMember]
public string name { get; set; }
}
[DataContract]
class ProficiencyType
{
[DataMember]
public string level { get; set; }
[DataMember]
public string name { get; set; }
}
[DataMember(Name="id")]
public int Id { get; set; }
[IgnoreDataMember] // Do not serialize this property
public string Name { get; set; }
[IgnoreDataMember]
public string ProficiencyLevel { get; set; }
[IgnoreDataMember]
public string ProficiencyName { get; set; }
[DataMember(Name="language")] // serialize this nested class property with name "language"
[JsonProperty(ObjectCreationHandling=ObjectCreationHandling.Replace)] // When deserializing, always create a fresh instance instead of reusing the proxy class.
NamedType LanguageName
{
get
{
return new NamedType { name = Name };
}
set
{
Name = (value == null ? null : value.name);
}
}
[DataMember(Name = "proficiency")]
[JsonProperty(ObjectCreationHandling = ObjectCreationHandling.Replace)]
ProficiencyType Proficiency
{
get
{
return new ProficiencyType { level = ProficiencyLevel, name = ProficiencyName };
}
set
{
ProficiencyLevel = (value == null ? null : value.level);
ProficiencyName = (value == null ? null : value.name);
}
}
}
If you find the opt-in nature of DataContract attributes to be a nuisance and prefer to use Json.NET-specific attributes, then the following is equivalent:
public class Language
{
class NamedType
{
public string name { get; set; }
}
class ProficiencyType
{
public string level { get; set; }
public string name { get; set; }
}
[JsonProperty(PropertyName = "id")]
public int Id { get; set; }
[JsonIgnore]
public string Name { get; set; }
[JsonIgnore]
public string ProficiencyLevel { get; set; }
[JsonIgnore]
public string ProficiencyName { get; set; }
[JsonProperty(PropertyName = "language", ObjectCreationHandling = ObjectCreationHandling.Replace)]
NamedType LanguageName
{
get
{
return new NamedType { name = Name };
}
set
{
Name = (value == null ? null : value.name);
}
}
[JsonProperty(PropertyName = "proficiency", ObjectCreationHandling = ObjectCreationHandling.Replace)]
ProficiencyType Proficiency
{
get
{
return new ProficiencyType { level = ProficiencyLevel, name = ProficiencyName };
}
set
{
ProficiencyLevel = (value == null ? null : value.level);
ProficiencyName = (value == null ? null : value.name);
}
}
}

Related

Error deserializing json with nested arrays

I'm trying to deserialize a json object that includes nested arrays and I get the following error when I try it in Postman:
{
"errors": {
"accounts": [
"Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Orders.Dtos.Accounts' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.\r\nPath 'accounts', line 7, position 17."
]
},
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-92466567188264cbdc71e6f6d7479688-fe08150cd947ddf1-00"
}
Simplified json to deserialize:
{
"plannedShippingDateAndTime": "2023-02-07T07:06:00 GMT+01:00",
"pickup": {
"isRequested": false
},
"productCode": "N",
"accounts": [
{
"typeCode": "shipper",
"number": "999999999"
}
]
}
Classes:
public class ShipmentData
{
public string PlannedShippingDateAndTime { get; set; }
public Pickup Pickup { get; set; }
public string ProductCode { get; set; } = "N";
public Accounts Accounts { get; set; }
}
public class Pickup
{
public bool isRequested { get; set; }
}
public class Accounts
{
public ArrayAccounts[] ArrayAccounts { get; set; }
}
public class ArrayAccounts
{
public string TypeCode { get; set; }
public string Number { get; set; }
}
Api Controller:
[HttpPost("CreateShipment/{OrderId}")]
public async Task<ActionResult> CreateShipment(string OrderId, ShipMentData shipmentData)
{
...
}
public class ShipmentData
{
public string PlannedShippingDateAndTime { get; set; }
public Pickup Pickup { get; set; }
public string ProductCode { get; set; }
public Account[] Accounts { get; set; }
}
public class Pickup
{
public bool IsRequested { get; set; }
}
public class Account
{
public string TypeCode { get; set; }
public string Number { get; set; }
}
Also Customize JSON binding:
options.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;

Get item from JSON Odata list for UWP

I have been having a hard time trying to figure this out that I've just about torn out all my hair now.
Using this section of code (Added a var result that I looking at with a stoppoint):
public async Task<string> GetHttpSPContentWithToken(string url, string token)
{
var httpClient = new System.Net.Http.HttpClient();
System.Net.Http.HttpResponseMessage response;
try
{
var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, url);
//Add the token in Authorization header
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
response = await httpClient.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<SharePointListItems.Fields>(content);
return content;
}
catch (Exception ex)
{
return ex.ToString();
}
}
The content that I receive is this (updated getting rid of extra information):
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites('root')/lists('FBA0AB63-8453-4BB9-AA17-142A5D72A50D')/items/$entity",
"#odata.etag": "\"60d40002-0f08-4f29-afa7-0287137b863b,1\"",
"createdDateTime": "2018-08-07T14:28:47Z",
"eTag": "\"60d40002-0f08-4f29-afa7-0287137b863b,1\"",
"id": "1",
"lastModifiedDateTime": "2018-08-07T14:28:47Z",
"webUrl": "https://XXXX.sharepoint.com/Lists/TestList/1_.000",
"createdBy": {
"user": {
"email": "XXXX#XXXX.onmicrosoft.com",
"id": "b5f81cc6-f8b7-46b7-8e10-6ce1b9689c23",
"displayName": "TK"
}
},
"lastModifiedBy": {
"user": {
"email": "XXXX#XXXX.onmicrosoft.com",
"id": "b5f81cc6-f8b7-46b7-8e10-6ce1b9689c23",
"displayName": "TK"
}
},
"parentReference": {},
"contentType": {
"id": "0x010001403BD420356E4ABE3B63E5AEC0713D"
},
"fields#odata.context": "https://graph.microsoft.com/v1.0/$metadata#sites('root')/lists('FBA0AB63-8453-4BB9-AA17-142A5D72A50D')/items('1')/fields/$entity",
"fields": {
"#odata.etag": "\"60d40002-0f08-4f29-afa7-0287137b863b,1\"",
"Title": "1",
"UserName": "TK",
"UserAge": "47",
"UserTitle": "Developer"
}
}
I just want the values forUserAge, UserName, and UserTitle to put each into a textbox, but not sure how to pull them out.
I am pretty sure that I need to set up a class of some sort, but it is the #odata parts that are breaking my back.
Everything that I have tried just gives me back a null value. I see the value there, just not sure how to parse/pull it out.
I have looked at this (updated):
using Newtonsoft.Json;
using System;
public class SharePointListItems
{
public class UserCreated
{
public string email { get; set; }
public string id { get; set; }
public string displayName { get; set; }
}
public class CreatedBy
{
public UserCreated user { get; set; }
}
public class UserModified
{
public string email { get; set; }
public string id { get; set; }
public string displayName { get; set; }
}
public class LastModifiedBy
{
public UserModified user { get; set; }
}
public class ParentReference
{
}
public class ContentType
{
public string id { get; set; }
}
public class Fields
{
[JsonProperty("#odata.etag")]
public string ODataETag { get; set; }
public string Title { get; set; }
public string UserName { get; set; }
public string UserAge { get; set; }
public string UserTitle { get; set; }
}
public class RootObject
{
[JsonProperty("#odata.context")]
public string ODataContext { get; set; }
[JsonProperty("#odata.etag")]
public string ODataETag { get; set; }
public DateTime createdDateTime { get; set; }
public string eTag { get; set; }
public string id { get; set; }
public DateTime lastModifiedDateTime { get; set; }
public string webUrl { get; set; }
public CreatedBy createdBy { get; set; }
public LastModifiedBy lastModifiedBy { get; set; }
public ParentReference parentReference { get; set; }
public ContentType contentType { get; set; }
[JsonProperty("fields#odata.context")]
public string FieldsODataContext { get; set; }
public Fields fields { get; set; }
}
}
But then I run into the issue that there is two [JsonProperty("#odata.etag")].
The [JsonProperty] custom attribute is added to the C# property that actually holds that value. Instead of putting the attribute on the Title or createDateTime property, you need to put them on their own properties:
public class RootObject
{
[JsonProperty("#odata.context")]
public string ODataContext { get; set; }
[JsonProperty("#odata.etag")]
public string ODataETag { get; set; }
// No attribute needed here
public DateTime createdDateTime { get; set; }
// etc...
Also, you are trying to parse the content as a Fields class, but it is a RootObject; you need to use
JsonConvert.DeserializeObject<SharePointListItems.RootObject>(content)
To get the object.

JSON. A reference deserializes to null

I am using Newtonsoft.json. I can't understand result of a deserialization. Bulletin.PageList is being filled ok. But Page of Question which is referencing to element of PageList is always null. In the JsonSerializerSettings I specified PreserveReferencesHandling.All but it didn't help. Could you help to resolve the problem of null of Page? Thank you
JSON structure:
"Bulletins": [
{
"$id": "46b5efa80fe644d7bd525e2c30f5df8a",
"$type": "Bulletin",
"JSONNETTYPE": "Bulletin",
"PagesList": [
{
"id": "4ed13d727cd144d1acf1e0c9bc273245",
"JSONNETTYPE": "PageView",
"Number": 1,
"Id": "1a2b8ed4249948e194b396c46a5d1eeb",
"UiId": "4ed13d727cd144d1acf1e0c9bc273245"
}
],
"AgendaQuestions": [
{
"$id": "eceb6fe6c74a40d59f0673b76bd6dbb3",
"$type": "QSimple",
"Page": {
"$ref": "46b5efa80fe644d7bd525e2c30f5df8a#PagesList.0"
}
}
]
}]
C# structure:
public class Bulletin
{
public Bulletin()
{
}
public string Dbid { get; set; }
public List<PageView> PagesList;
public List<Question> AgendaQuestions;
}
public abstract class Question
{
protected Question(int number, string customNumberLabel = null)
{
Number = number;
CustomNumberLabel = customNumberLabel;
}
public int Number { get; set; }
public string CustomNumberLabel { get; set; }
public PageView Page { get; set; }
}
public class PageView
{
public int Number { get; set; }
public string Id { get; set; }
public int BulletinNumber { get; set; }
public PageView()
{
}
}
var settings = new JsonSerializerSettings
{ PreserveReferencesHandling = PreserveReferencesHandling.All
}
I was using dojox.json.ref library. If I set __id for Page, references to elements of PagesList will become direct, not complex (like 46b5efa80fe644d7bd525e2c30f5df8a#PagesList.0), and Page is filled.

How to display JSON data in Xamarin.forms list view?

I am new to xamarin.forms, and i want to build an cross platform app that will display JSON data in the list view. This is my JSON data.
{
"fish_product": [
{
"fish_id": "1",
"fish_img": "",
"fish_name": "Indian Mackerel",
"fish_category": "Marine Fish",
"size": "Medium",
"price": "100"
},
{
"fish_id": "2",
"fish_img": "",
"fish_name": "Manthal Repti",
"fish_category": "Marine Fish",
"size": "Small",
"price": "200"
},
{
"fish_id": "4",
"fish_img": "",
"fish_name": "Baby Sole Fish",
"fish_category": "Marine Fish",
"size": "Small",
"price": "600"
}
]
}
I want to display "fish_name" in list view. please suggest any solution or any article or tutorial for this. thank you in advance.
I think you have to deserialize this JSON to an object like
public class FishProduct
{
public string fish_id { get; set; }
public string fish_img { get; set; }
public string fish_name { get; set; }
public string fish_category { get; set; }
public string size { get; set; }
public string price { get; set; }
}
public class RootObject
{
public ObservableCollection<FishProduct> fish_product { get; set; }
}
Then you have to use a ListView where you set your listview.ItemsSource = myList.fish_product
Then you have to create a DataTemplate using ViewCell
In this ViewCell you can have a Label when you SetBinding the fish_name field. In code something like
Label label = new Label();
label.SetBinding(Label.TextProperty, "fish_name");
I think you can take a look to ListView Documents
I think this may help you
Your MODEL
public class FishProduct
{
public string fish_id { get; set; }
public string fish_img { get; set; }
public string fish_name { get; set; }
public string fish_category { get; set; }
public string size { get; set; }
public string price { get; set; }
}
public class RootObject
{
public List<FishProduct> fish_product { get; set; }
}
Webservice call and JSON Deserialisation
public async Task GetData()
{
try
{
HttpClient client = new HttpClient();
var result = await client.GetAsync("http://yourJSON_Url");
result.EnsureSuccessStatusCode();
string json = await result.Content.ReadAsStringAsync();
List<FishProduct> res= JsonConvert.DeserializeObject<List<FishProduct>>(json);
}
catch (Exception ex)
{
throw;
}
}

Newtonsoft.Json serialization of PagedList<T> is not including some properties

I am trying to serialize a PagedList object ( https://github.com/martijnboland/MvcPaging/blob/master/src/MvcPaging/PagedList.cs ) to Json, like this:
PagedList<Product> pagedList = new PagedList<Product>(products, (page - 1), pageSize);
string json = Newtonsoft.Json.JsonConvert.SerializeObject(pagedList);
If I use the above code, in the result I get an array of Product objects serialized properly. However the properties below (of PagedList) are not being included in the Json result:
public bool HasNextPage { get; }
public bool HasPreviousPage { get; }
public bool IsFirstPage { get; }
public bool IsLastPage { get; }
public int ItemEnd { get; }
public int ItemStart { get; }
public int PageCount { get; }
public int PageIndex { get; }
public int PageNumber { get; }
public int PageSize { get; }
public int TotalItemCount { get; }
They are not being serialized but they are part of PagedList.
Does anyone know why? And how could I include those properties in the serialization?
Thanks
The serializer sees that PagedList is enumerable, so it serializes it to a JavaScript array. To make this easier to deal with I expose a GetMetaData() function on the PagedList object that will return a MetaData object containing exactly the fields you mentioned above. This means you can serialize your pagedlist like so:
string json = Newtonsoft.Json.JsonConvert.SerializeObject(new{
items = pagedList,
metaData = pagedList.GetMetaData()
});
This should result in a JSON object like so:
{
"Items": [
{ ... },
{ ... },
{ ... }
],
"MetaData": {
"PageSize": 1,
"PageCount": 2,
"HasNextPage": true,
...
}
}
there is a jquery plugin which is used to excitely to do this :
https://github.com/turhancoskun/pagify.mvc
<script type="text/javascript">
$(function() {
$('#users').pagify({
dataUrl: '/User/UserLis',
callBack: function(){
// Ajax remove preloader and some other callbacks
},
beforeSend: function(){
// Ajax show preloader and some other function before start
}
});
}
</script>
readme.md file contains an example
To serialize, just use the Troy's implementation. But to deserialize create 2 new classes:
public class PaginationEntity<TEntity> where TEntity : class
{
public PaginationEntity()
{
Items = new List<TEntity>();
}
public IEnumerable<TEntity> Items { get; set; }
public PaginationMetaData MetaData { get; set; }
}
public class PaginationMetaData
{
public int Count { get; set; }
public int FirstItemOnPage { get; set; }
public bool HasNextPage { get; set; }
public bool HasPreviousPage { get; set; }
public bool IsFirstPage { get; set; }
public bool IsLastPage { get; set; }
public int LastItemOnPage { get; set; }
public int PageCount { get; set; }
public int PageNumber { get; set; }
public int PageSize { get; set; }
public int TotalItemCount { get; set; }
}
and deserialize this way:
PaginationEntity<TEntity> listPaginated = JsonConvert.DeserializeObject<PaginationEntity<TEntity>>(json)
and you are good to go.
By default asp.core uses DataContractSerializer, so anyone could set [DataContract] and [DataMember] to Product class and members and get needed data with following action:
public IActionResult Products(int page=1,int countPerPage=10)
{
var result = _dbContext.Products.AsNoTracking().ToPagedList(page, countPerPage);
return Ok( new{result, total=result.TotalItemCount });
}
with no need to use Newtonsoft.Json